BroadcastReceiver开机启动Service,并在service中启动Notification,不能正确运行

skj1995 2017-10-29 08:17:50
我想实现一个BroadcastReceiver开机启动Service,并在service中启动Notification的功能,但是不能正常运行。这是我的代码,大佬们能不能帮我看看为什么:

public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
System.out.println( "开机自动服务自动启动.....");
}
@Override
protected void onHandleIntent(Intent intent) {
getNotification();
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d("Demo","onDestroy 已执行");
}
public void getNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.tickerText = "来消息了";
notification.icon = R.mipmap.image2;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults =Notification.DEFAULT_VIBRATE;
long when = System.currentTimeMillis();
notification.when = when;
Intent intent = new Intent(getApplication(), FoodDetailed.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Bmob Test");
builder.setContentText("IM start up");
builder.setSmallIcon(R.drawable.img1);
//Notification builder = new Notification.Builder(this).setTicker("显示于屏幕顶端状态栏的文本").setSmallIcon(R.mipmap .image2).build();
manager.notify(0, notification);
}
}


public class DeviceStartedListener extends BroadcastReceiver {
public DeviceStartedListener() {
}


@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "开机自动服务自动启动.....");
//后边的XXX.class就是要启动的服务
Intent serviceIntent = new Intent(context, UpdateService.class);
context.startService(serviceIntent);

System.out.println( "开机自动服务自动启动.....");
}
}
...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
辉_alexxm 2017-10-31
  • 打赏
  • 举报
回复
有开机自启动吗,好像没有看到代码? 可以去度娘下
harry.gong 2017-10-31
  • 打赏
  • 举报
回复
log...
  • 打赏
  • 举报
回复
为什么我回复的没显示出来。你的广播注册了没?静态Or动态

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧