Android 应用程序从后台删除后,广播接收器内部服务未侦听
我有一个广播接收器注册,以接收服务内的短信。该应用程序的目的是在接收时获取SMS,并将来自预期发件人的SMS内容保存在Sqlite存储中。即使应用程序未在后台运行,应用程序也需要捕获收到的短信Android 应用程序从后台删除后,广播接收器内部服务未侦听,android,broadcastreceiver,android-service,android-broadcastreceiver,android-sms,Android,Broadcastreceiver,Android Service,Android Broadcastreceiver,Android Sms,我有一个广播接收器注册,以接收服务内的短信。该应用程序的目的是在接收时获取SMS,并将来自预期发件人的SMS内容保存在Sqlite存储中。即使应用程序未在后台运行,应用程序也需要捕获收到的短信 public class SMSService extends Service { private final BroadcastReceiver receiver = new BroadcastReceiver() { public static final String SM
public class SMSService extends Service {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public static final String SMS_BUNDLE = "pdus";
ContentValues userValues;
Object[] sms;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
Log.d("~~~ SMS !!!", "~~~ SMS ~~~ SMS ~~~ SMS~~~ SMS ~~~");
AsyncSmsHandler asyncSmsHandler = new AsyncSmsHandler(context, intent);
asyncSmsHandler.execute("");
}
}
class AsyncSmsHandler extends AsyncTask<String, Void, String> {
Context context;
Intent intent;
public AsyncSmsHandler(Context context, Intent intent) {
this.context = context;
this.intent = intent;
}
@Override
protected String doInBackground(String... params) {
String smsBody = "", address = "";
Bundle intentExtras = intent.getExtras();
SmsMessage smsMessage = null;
if (intentExtras != null) {
sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
smsBody = smsBody + smsMessage.getMessageBody();
smsMessageStr += smsBody + "\n";
}
address = smsMessage.getOriginatingAddress();
Log.d("SMS RECEIVER NUMBER", " " + smsBody + " -- " + sms.length);
//SAVE THE MESSAGE AND NUMBER
}
return "";
}
@Override
protected void onPostExecute(String result) {
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(context, AquaOrderList.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
.setSmallIcon(R.drawable.logo)
.setContentTitle(“TITLE”)
.setContentText(“Message”)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, builder.build());
}
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
}
}
公共类SMSService扩展服务{
专用最终BroadcastReceiver=新的BroadcastReceiver(){
公共静态最终字符串SMS_BUNDLE=“PDU”;
ContentValues用户值;
对象[]短信;
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(action.equals(“android.provider.Telephony.SMS_RECEIVED”)){
Log.d(“~~~SMS!!!”,“~~~SMS~~~~~~SMS~~~~~”;
AsyncSmsHandler AsyncSmsHandler=新的AsyncSmsHandler(上下文,意图);
asyncSmsHandler.execute(“”);
}
}
类AsyncSmsHandler扩展AsyncTask{
语境;
意图;
公共AsyncSmsHandler(上下文、意图){
this.context=上下文;
本意=本意;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串smsBody=“”,address=“”;
Bundle intentExtras=intent.getExtras();
SmsMessage SmsMessage=null;
if(intentExtras!=null){
sms=(对象[])intentExtras.get(sms_BUNDLE);
字符串SMSMessagester=“”;
对于(int i=0;i
通过这种方式,在onCreate中注册接收方,在onDestroy方法中取消注册接收方
这项工作如预期,但当我关闭应用程序后,如果我收到短信,它不会被触发。如果我再次打开应用程序,在手机短信开始储存在存储器中之后
服务被破坏了吗?我在手机上查看了“正在运行的服务”,我可以看到服务正在运行
我还在清单文件中添加了权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.app”>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".SMSService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".OrderList"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_order_list"
android:theme="@style/FullscreenTheme"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我并不完全同意@Ranjan的评论。是的,服务是免费的
被安卓摧毁并继续运行&是的,运行一个
永久服务。但是如果你一切都做对了,
您的服务被破坏多少次,它就会被启动
再次(自动)启动时,您的接收器应
注册的
我的建议是:
- 首先,确保您的目标不是最新的SDK。我使用的是targetSDK 10、minSDK 17和compileSDK 26。这可能会对您的服务的处理方式产生影响。使用这些值来检查是否仅此一项就可以工作
- 接下来,请遵循此线程并尝试正确创建接收器
- 我不确定你的问题到底是什么,但是我可以告诉你,我们有一个视频会议应用程序,它可以监听来电和消息。所以,当然,广播接收器需要注册,服务应该在后台运行。这个应用程序有点旧,是用targetSDK 10等开发的。所以他们用当时常见的方式来做。我们还没有移植我们的应用程序,所以我们不知道新安卓系统存在什么问题。正如我提到的,我们的应用程序使用SDK26编译
代码示例:
public SampleService() {
super();
Log.d(TAG, "SampleService: Constructor");
final HandlerThread mHandlerThread = new HandlerThread(BR_THREAD_NAME, Process.THREAD_PRIORITY_BACKGROUND);
mHandlerThread.start();
bgLooper = mHandlerThread.getLooper();
mBRHandler = new Handler(bgLooper);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
registerReceiver();
}
private void registerReceiver() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SOME_ACTION);
myReceiver = new MyReceiver();
registerReceiver(myReceiver, intentFilter, null, mBRHandler);
}
这应该足够好了您必须使用清单声明的接收者,但我没有使用独立的接收者。我正在服务中添加IntentFilter及其操作。当应用程序位于前台时,它就会工作。还有我