简体   繁体   English

当应用程序在奥利奥中被杀死时,广播接收器不工作为什么?

[英]Broadcast Receiver not working when the App killed in Oreo Why?

How can i receive data of incoming message using broadcast receiver in Oreo, its working perfect in before Oreo version,but i am unable to receive in Oreo, i have trying to short out this by help of developer site but there is not any sample code for this only Oreo limitation is given there我如何使用 Oreo 中的广播接收器接收传入消息的数据,它在 Oreo 版本之前完美运行,但我无法在 Oreo 中接收,我试图通过开发人员站点的帮助来缩短它,但没有任何示例代码为此,那里只给出了奥利奥限制

Here is my BroadCast Receiver Class这是我的广播接收器类

public class SMSReceiver extends BroadcastReceiver


{
String sender,message;
public void onReceive(Context context, Intent intent) {

    Bundle myBundle = intent.getExtras();
    SmsMessage[] messages = null;
    String strMessage = "";

    if (myBundle != null) {
        Object[] pdus = (Object[]) myBundle.get("pdus");
        messages = new SmsMessage[pdus.length];

        SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
        for (int i = 0; i < messages.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            sender=messages[i].getOriginatingAddress();

            String message =shortMessage.getMessageBody();



        Toast.makeText(context, sender+"\n"+message, Toast.LENGTH_SHORT).show();


        }

    }

}

and here is my Manifest这是我的清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:roundIcon="@drawable/logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <receiver android:name=".SMSReceiver" android:enabled="true" android:exported="true">
        <intent-filter
            android:priority="1000"
            >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>


    </service>

</application>

From Android Oreo onwards, most Broadcast receivers need to be registered on runtime instead of the manifest declaration.从 Android Oreo 开始,大多数广播接收器需要在运行时注册而不是清单声明。

BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        //Do Something
    }
};

Then register the receiver:然后注册接收器:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.THE_REQUIRED_ACTION);
registerReceiver(myReceiver, intentFilter);

and unregister:并注销:

unregisterReceiver(myReceiver);

You can register /unregister the receivers at runtime, by adding the code above to onResume()/ onPause() respectively.您可以在运行时注册/取消注册接收器,方法是将上面的代码分别添加到 onResume()/onPause() 中。

If you want the receiver to persist even if the app is in the background you can register/unregister in your application class instead.如果您希望接收器即使应用程序在后台也能持续存在,您可以改为在您的应用程序类中注册/取消注册。 If you want it to persist after the user exits the app, you need to register the receiver inside a service or job scheduler.如果您希望它在用户退出应用程序后持续存在,您需要在服务或作业调度程序中注册接收器。

I think it could be because you also need to add it as a permission , like this:我认为这可能是因为您还需要将其添加为权限,如下所示:

<receiver
    android:name=".SMSReceiver"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter android:priority="1000">
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM