Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>

like image 426
Basant Avatar asked Jul 02 '18 09:07

Basant


1 Answers

From Android Oreo onwards, most Broadcast receivers need to be registered on runtime instead of the manifest declaration.

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.

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.

like image 62
Nikos Hidalgo Avatar answered Oct 11 '22 12:10

Nikos Hidalgo