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

时间:2018-07-02 09:07:06

标签: java android android-broadcastreceiver android-8.0-oreo

我如何在Oreo中使用广播接收器接收传入消息的数据,它在Oreo版本之前可以正常工作,但是我无法在Oreo中接收,我已经尝试通过开发者网站来简化此操作,但是没有在那里给出了仅此Oreo限制的任何示例代码

这是我的BroadCast接收器类

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();


        }

    }

}

这是我的清单

<?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>

2 个答案:

答案 0 :(得分:1)

从Android Oreo开始,大多数广播接收器都需要在运行时注册,而不是清单声明。

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

};

然后注册接收者:

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

并注销:

unregisterReceiver(myReceiver);

您可以在运行时注册/取消注册接收者,方法是分别将上述代码添加到onResume()/ onPause()。

如果即使应用程序在后台,您也希望接收者继续存在,则可以在应用程序类中注册/注销。 如果您希望它在用户退出应用程序后仍然存在,则需要在服务或作业计划程序中注册接收器。

答案 1 :(得分:0)

我认为可能是因为您还需要将其添加为permission,如下所示:

Sub OSP_UntouchedReport()

Dim Magisk As Long
With ActiveSheet
Magisk = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

Sheets("RAW").Select
Range("BI1").Value = ("LDAP MAP")
Range("BI2").FormulaR1C1 = "=VLOOKUP(RC[-2],Reference!C[-52]:C[-51],2,0)"
Range("BI2").AutoFill Destination:=Range("BI2:BI" & Magisk)
Range("BI2:BI" & Magisk).Copy
Range("BI2:BI" & Magisk).PasteSpecial xlPasteValues
    Application.CutCopyMode = False

Range("BI1").AutoFilter Field:=61, Criteria1:="#N/A"
Range("A1:BI1" & Magisk).Copy
Sheets.Add.Name = "NA"
Sheets("NA").Paste
    Application.CutCopyMode = False

Sheets("RAW").Select
Range("BI1").AutoFilter Field:=61, Criteria1:="<>#N/A"
Range("A1:BI1" & Magisk).Copy
Sheets.Add.Name = "MAPPED"
Sheets("MAPPED").Paste
    Application.CutCopyMode = False

Sheets("RAW").Select
Cells.AutoFilter