【发布时间】:2016-08-22 08:55:55
【问题描述】:
我有一个在前台运行的服务,在这个服务中我有一个广播接收器,它监听 SMS_RECEIVED 操作。
当用户在应用程序内部(应用程序和服务都在前台)时,一切正常,我正在接收意图。
但是当用户存在应用程序时(只有带广播的服务在前台),收到短信时服务停止。
当服务停止时,任何地方都找不到错误报告(不在 logcat 中,也不会弹出崩溃对话框)。
我的广播服务:
public class myService extends IntentService {
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(myService.this, intent.getAction(), Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate() {
super.onCreate();
IntentFilter i= new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
i.setPriority(999);
registerReceiver(mReceiver, receiverFilter);
startForeground(...);
}
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
}
我的清单中也有以下权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
myService 在清单中是这样声明的:
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="false" />
【问题讨论】:
-
你能显示有错误的 logcat 吗?
-
嘿@MasterDisaster,logcat 显示没有错误。
-
foreground service crash所以崩溃或不崩溃。如果崩溃,你应该在 logcat 中有错误。 -
它崩溃(服务停止),但 logcat 中没有错误。
-
哦,我刚看到这个 -
myService extends IntentService。您想为此使用常规的Service。IntentServices 旨在处理有限的任务,然后自行停止。 (顺便说一句,当 cmets 中有多个其他用户时,除非您使用 @username 将他们发送给特定用户,否则没有人会收到有关您的 cmets 的通知。我只是碰巧回来检查。仅供参考。没有忽略你。:-)