[英]SMS BroadcastReceiver stops working after sometime
I have an app which does some task once it receives a SMS.我有一个应用程序,它会在收到短信后执行一些任务。 I have implemented it using BroadcastReceiver.
我已经使用 BroadcastReceiver 实现了它。 After installing the app, it works fine for sometime.
安装应用程序后,它可以正常工作一段时间。
Afterwards, I notice that onReceive event of BroadcastReceiver is not triggered.之后,我注意到没有触发 BroadcastReceiver 的 onReceive 事件。 To troubleshoot, I restarted my app service and connected the mobile to Android Studio Logcat.
为了进行故障排除,我重新启动了我的应用服务并将手机连接到 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.
当我在 30 分钟后发送短信时,没有触发 onReceive 事件。 There was no messages related to my app during that 30 mins in Logcat.
在 Logcat 的那 30 分钟内,没有与我的应用程序相关的消息。
Looks like BroadcastReceiver service is killed.看起来 BroadcastReceiver 服务被杀死了。 What could be the reason.
可能是什么原因。 How can I troubleshoot?
我该如何排除故障? I am testing this app in Android 8.1.
我正在 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);
}
}
}
The issue was the the EMUI, MIUI etc were killing the background tasks.问题是 EMUI、MIUI 等正在杀死后台任务。 After whitelisting the app, it works fine now.
将应用程序列入白名单后,它现在可以正常工作了。
白名单只需转到[miui手机]应用程序>>管理应用程序>>[选择白名单应用程序]>>省电>>选择“无限制”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.