您是否可以实现在后台运行的Android应用程序,以便在不点击应用程序图标的情况下打开某个事件?
答案 0 :(得分:0)
我们可以使用广播接收器启动应用并定义意图动作:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/** This class is used to register the broadcast receiver
* as soon as device boot completes this broadcast receiver functionality comes into function and you can launch your application */
public class HomeScreenLaunch extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent launch = new Intent(context, ActivityName.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launch);
}
}
}
以下是在清单文件中定义此广播接收器的代码:
<receiver
android:name="com.xyz.HomeScreenLaunch"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
在要通过事件侦听器启动的OnCreate活动方法中,您需要添加以下代码。这将唤醒设备并自动解锁以自行启动。
KeyguardManager.KeyguardLock lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
PowerManager powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
PowerManager.WakeLock wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
lock.disableKeyguard();
wake.acquire();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accept_reject);
用于收听事件广播接收器的使用。它们有两种类型:1)静态注册(需要清单权限) 2)动态注册(不需要清单权限) 对于第一种类型,您可以参考Rohit Sharma先生的回答。 对于第二种类型,我附上以下代码。
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsService extends Service {
private String Sender=null;
private String Code=null;
public void setCode(String code){
Code=code;
}
public void setSender(String sender){
Sender=sender;
}
private IBinder mBinder = new MyBinder();
private BroadcastReceiver smsReceiver;
private static final String ACTION="android.provider.Telephony.SMS_RECEIVED";
@Override
public void onCreate(){
smsReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do whatever you need it to do when it receives the broadcast
// Example show a Toast message...
Bundle extras = intent.getExtras();
Log.i("Receiver","broadcast received");
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
String origNumber = msg.getOriginatingAddress();
String msgBody = msg.getMessageBody();
Log.i("Receiver",origNumber);
Log.i("Receiver",msgBody);
Log.i("Receiver", intent.toString());
if(Sender!=null && origNumber.contains(Sender)){
if(Code!=null && msgBody.contains(Code)){
Log.i("Receiver","matched");
}
}
}
};
final IntentFilter theFilter = new IntentFilter();
theFilter.addAction(ACTION);
theFilter.setPriority(2147483647);
this.registerReceiver(this.smsReceiver, theFilter);
Log.i("Received","service");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
public class MyBinder extends Binder {
public SmsService getService() {
return SmsService.this;
}
}
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(smsReceiver);
}
}