总体思想是:接收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)
个人理解好像是你的service 没有启动吧,你的startService() 可以写在activity.onCreate() 里。
而且安全问题起码需要启动你的APP 一次才行。
你可以这样验证:
1. 在手机里卸载掉你的APP,
2. 通过 adb install 来安装你的APP, 这样就可以模拟用户安装软件的场景,因为用户安装软件,软件是需要用户去手动启动的。
3. 去程序管理看看你的Service 有没有启动, 没有的话,你可以打开你的 App来启动Service.
4. 然后重启手机,再去看看你的Service 有没有在跑。
当然如果你的App 有取得root 权限,就不一样。
还有一个是从6.0 开始,安卓需要对用户在APP里对 "危险的权限“ 进行逐一授权, 而我没记错的话,SMS 和 Call 就是危险权限。
有一个解决办法就是你用比较低的API, 记得某一个API 之前安卓的权限管理是很薄弱的,具体是哪一个我忘了。
首先确定你收到了开机广播。
手机由于安全原因,开机启动不是注册了广播就会有效的,你可以试试用手机的安全卫士允许你的app开机启动,可能安全软件拦截了
谢谢您的讲解,果然是需要启动一次service.
这种情况一步步打log就好了 先看广播有没有接受 在看服务有没有启动 在看有没有运行到发通知的代码