本文介绍了广播接收器可在模拟器上运行,但不能在真实设备上运行(Android Oreo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
广播接收器在我杀死我的应用程序时可以完美地工作,它可以在后台工作.但是我安装了我的真实设备,当我杀死应用程序时并没有.
Broadcast receiver it's work perfectly when I kill my application it work in background. but i installed my real device it's don't when i kill application.
> 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"/>
似乎是隐式广播.尝试使用 intentFilter.addAction(您的操作)
seems to be implicit broadcast. Try registering them in your activity class using intentFilter.addAction(Your Action)
还通过 onStop 取消注册接收者呼叫 unregisterReceiver(接收器实例)
Also unregister you receiver in onStop bycalling unregisterReceiver(Receiver instance)
这篇关于广播接收器可在模拟器上运行,但不能在真实设备上运行(Android Oreo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!