广播接收机,当我杀死我的应用程序时,它可以在后台正常工作。但是我安装了我的真实设备,当我杀死应用程序时不是。
> Java Code
public class BackgroundService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Send Message",Toast.LENGTH_LONG).show();
}
}
> MainActivity
BackgroundService backgroundService;
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(backgroundService,intentFilter);
}
> Manifests
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<receiver android:name=".BackgroundService">
<intent-filter android:priority="999">
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
[从android 8开始,如果您有任何隐式广播,则必须将其注册到不在清单中的活动中您的情况
<action android:name="android.intent.action.SCREEN_ON"/> <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
``` seems to be implicit broadcast. Try registering them in your activity class using **intent.addAction(Your Action)**
Also unregister you receiver in **onStop** by
calling **unregisterReceiver(Receiver instance)**