qq_37839942 2017-03-10 00:30 采纳率: 100%
浏览 1056
已采纳

android开发。希望开机发送一个notification,未能实现。

总体思想是:接收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)

  • 写回答

4条回答 默认 最新

  • qq_31244055 2017-03-10 01:47
    关注

    个人理解好像是你的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 之前安卓的权限管理是很薄弱的,具体是哪一个我忘了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料