Android8.0+跨应用发送广播和跨应用启动前台服务

Android8.0开始发送广播方式以及启动服务的方式有变更,旧的方式已失效。

新的方式如下:

发送方:

1. 发送前台广播需要权限  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

2-1 接收方为动态注册的广播,这样发送:

Intent intentTest = new Intent();
intentTest.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentTest.setAction("你的Action");
sendBroadcast(intentTest);

2-2 接收方为静态注册的广播,这样发送:

Intent intentTwo = new Intent();
intentTwo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentTwo.setAction(TEST_ACTION_2);
intentTwo.setComponent(new ComponentName("包名", "包名+ 类名如MyBroadcastReceiver"));//完整路径
sendBroadcast(intentTwo);

3 启动前台服务:

Intent intent = new Intent();
intent.setAction("你的Action");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("包名", "包名+类名如MyService"));//完整路径
intent.setPackage("com.lzui.startservicetest");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}

接收方:

1. 动态广播代码注册即可

2. 静态广播

<receiver android:name="包名+.MyBroadcastReceiver"> <!--完整路径-->
<intent-filter>
<action android:name="你的Action" />
</intent-filter>
</receiver>

3. 服务

<service
android:name=".MyService"
android:exported="true">
<intent-filter>
<action android:name="你的Action" />
</intent-filter>
</service>

接收的服务需要设置前台服务,否则5秒后系统会报错,在onCreate或者onStartCommand里面都可以
public int onStartCommand(Intent intent, int flags, int startId) {
//静默通知
silentForegroundNotification();
}

private void silentForegroundNotification(Context context) {
//适配8.0service
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && notificationManager != null) {
mChannel = new NotificationChannel(CHANNEL_ID_STRING, "one", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
startForeground(1, notification);
}

//以下方式也可以 (不是静默)
/*String ID = "com.example.service1"; //这里的id里面输入自己的项目的包的路径
String NAME = "Channel One";
Intent intent = new Intent(MyService.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notification; //创建服务对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(ID, NAME, manager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(MyService.this).setChannelId(ID);
} else {
notification = new NotificationCompat.Builder(MyService.this);
}
notification.setContentTitle("标题")
.setContentText("内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
Notification notification1 = notification.build();
startForeground(1,notification1);*/
}

至此,适配OK,亲测有效。




原文地址:https://www.cnblogs.com/mengdao/p/15102375.html