SMS BroadcastReceiver stops working after sometime

旧时模样 提交于 2020-07-20 04:40:30

问题


I have an app which does some task once it receives a SMS. I have implemented it using BroadcastReceiver. After installing the app, it works fine for sometime.

Afterwards, I notice that onReceive event of BroadcastReceiver is not triggered. To troubleshoot, I restarted my app service and connected the mobile to Android Studio Logcat. As earlier, initially it detected new SMS and everything worked fine. When I sent SMS after 30 mins, onReceive event was not triggered. There was no messages related to my app during that 30 mins in Logcat.

Looks like BroadcastReceiver service is killed. What could be the reason. How can I troubleshoot? I am testing this app in Android 8.1.

My code below

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chickoo.whereareyou" >


    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.chickoo.whereareyou.IncomingSms"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <service
            android:name=".LocationUpdatesService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

Broadcast receiver class file

public class IncomingSms extends BroadcastReceiver {



    public void onReceive(Context context, Intent intent) {
        Log.i("SmsReceiver", "SMS Recd1");
        final Bundle bundle = intent.getExtras();

        try {
            Log.i("SmsReceiver", "SMS Recd2");

            if (bundle != null) {
                Log.i("SmsReceiver", "SMS Recd3");

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    Log.i("SmsReceiver", "SMS Recd5");

                    String senderNum = currentMessage.getDisplayOriginatingAddress();
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
                    if (message.equals("WAY")) {
                        Log.i("SmsReceiver", "GPSlocn service to be stared");
                            Intent intent1 = new Intent(context, LocationUpdatesService.class);
                            intent1.putExtra("SENDNUM", senderNum);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                context.startForegroundService(intent1);
                            } else {
                                context.startService(intent1);
                            }


                        Log.i("SmsReceiver", "GPSlocn service started");

                    }
                } 
            } 
        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);
        }
    }



}

回答1:


The issue was the the EMUI, MIUI etc were killing the background tasks. After whitelisting the app, it works fine now.



来源:https://stackoverflow.com/questions/53456784/sms-broadcastreceiver-stops-working-after-sometime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!