总体思想是:接收RECEIVE_BOOT_COMPLETED广播,启动一个service,在service内发送notification。
结果是开机启动没有发送notification,logcat里显示android.os.deadObjectException。
我在程序里的文本框用click事件启动service是可以发送notification的。
代码如下:
Mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.credao.repeater">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
android:name=".MainActivity"
android:label="@string/app_name">
<service
android:name=".SMSAndCallMonitorService"
android:enabled="true"
android:exported="true">
</service>
<receiver
android:name=".OnBootStartReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</receiver>
</application>
接收RECEIVE_BOOT_COMPLETED广播的接收器:
package com.credao.repeater;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootStartReceiver extends BroadcastReceiver {
public OnBootStartReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: start SMSAndCallMonitorService
Intent i=new Intent();
i.setClassName("com.credao.repeater","com.credao.repeater.SMSAndCallMonitorService");
context.startService(i);
}
}
Service:
package com.credao.repeater;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class SMSAndCallMonitorService extends Service {
public SMSAndCallMonitorService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("service", "SMSAndCallMonitorService onstartcommand");
Notification.Builder notification=new Notification.Builder(this);
notification.setContentTitle("SMSAndCallMonitorService")
.setContentText("onStartCommand()")
.setSmallIcon(R.drawable.ic_stat_name);
NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1,notification.build());
stopSelf();
return super.onStartCommand(intent,flags,startId);
}
}
Activity中可以正常启动service中notification的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View text=findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent();
i.setClassName("com.credao.repeater","com.credao.repeater.SMSAndCallMonitorService");
MainActivity.this.startService(i);
Log.w("text: ", "text is clicked");
}
});
}
错误提示:
2209-3577/? E/IndexSearchManager﹕ MSG_BOOTCOMPLETE mService.bootCompleted error
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:617)
at com.huawei.indexsearch.IndexSearchManager$IndexSearchManagerProxy.bootCompleted(IndexSearchManager.java:365)
at com.huawei.indexsearch.IndexSearchManager.handleBootCompleted(IndexSearchManager.java:150)
at com.huawei.indexsearch.IndexSearchManager.-wrap2(IndexSearchManager.java)
at com.huawei.indexsearch.IndexSearchManager$IndexSearchHandler.handleMessage(IndexSearchManager.java:137)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:156)
at android.os.HandlerThread.run(HandlerThread.java:61)