我写了一个服务用另外一个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 上不能启动
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 上不能启动
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货