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,親測有效。