oppo android 7.0 不动服务

jx315425246 2019-09-30 07:48:34
我写了一个服务用另外一个activity 跨进程启动


public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("服务运行", "onStartCommand: 开始");
Notification.Builder notification;
Intent mintent = new Intent(this.getApplicationContext(),outkeeplive.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


NotificationChannel channel=new NotificationChannel("com.zxb.service.channel","com.zxb.service.channel",NotificationManager.IMPORTANCE_LOW);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.createNotificationChannel(channel);

notification = new Notification.Builder(this, "com.zxb.service.channel");
} else {
notification = new Notification.Builder(this);
}
notification.setContentTitle("我的服务");
notification.setContentText("服务启动");
notification.setSmallIcon(R.drawable.xiangcheng);

notification.setContentIntent(PendingIntent.getActivity(this.getApplicationContext(), 0, mintent, 0));
notification.setOngoing(true);
startForeground(serviceId, notification.build());
Log.d("服务运行", "onStartCommand: 结束");
return START_REDELIVER_INTENT;
}


服务的androidmanifest.xml

<service
android:name=".myservice"
android:enabled="true"
android:exported="true"></service>



在 activity 中的调用


Intent intent = new Intent();
intent.setComponent(new ComponentName("com.zxb.service", "com.zxb.service.myservice"));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Log.d("开始服务", "onResume: com.zxb.service");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d("开始服务", "onResume: 前台服务");
startForegroundService(intent);
} else {
Log.d("开始服务", "onResume: 后台服务");
startService(intent);
}


在android 6.0上运行正常,而在 oppo android 7.0 上不能启动
...全文
216 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jx315425246 2019-10-03
  • 打赏
  • 举报
回复
有没有大神知道啊
jx315425246 2019-10-01
  • 打赏
  • 举报
回复
这是什么错误,运行还是老样子,在android 6.0上运行正常,而在 oppo android 7.0 上不能启动

80,471

社区成员

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

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