1.首先在activityj里面启动
Intent intent = new Intent(MainActivity.this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent );
} else {
startService(intent );
}
2.需要在前台运行的服务
public class MyService extends Service {
private static final String ID = "channel_1";
private static final String NAME = "前台服务";
private final int PID = android.os.Process.myPid();
private ServiceConnection mConnection;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//大于等于android O
setForeground();
} else {
setForegrounds();
}
return super.onStartCommand(intent, flags, startId);
}
@TargetApi(Build.VERSION_CODES.O)
private void setForeground() {//大于等于android O
Intent intent = new Intent();//跳转到应用信息
intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + PushService.this.getPackageName()));
PendingIntent pendingIntent = PendingIntent.getActivity(PushService.this, 0, intent, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(this, ID)
.setSmallIcon(R.mipmap.app_logo)
.setContentTitle(getResources().getString(R.string.foreground_title))
.setContentText(getResources().getString(R.string.foreground_content))
.setContentIntent(pendingIntent)//跳转到应用信息
.build();
startForeground(1, notification);// 开始前台服务
}
private void setForegrounds() {//小于android o 屏蔽前台通知
if (null == mConnection) {
mConnection = new CoverServiceConnection();
}
this.bindService(new Intent(this, HelpService.class), mConnection,
Service.BIND_AUTO_CREATE);
}
private class CoverServiceConnection implements ServiceConnection {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
// 2个service分别startForeground,然后只在1个service里stopForeground,这样即可去掉通知栏的显示
Service helpService = ((HelpService.LocalBinder) binder)
.getService();
PushService.this.startForeground(PID, getNotification());
helpService.startForeground(PID, getNotification());
helpService.stopForeground(true);
PushService.this.unbindService(mConnection);
mConnection = null;
}
}
private Notification getNotification() {
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + PushService.this.getPackageName()));//跳转到应用信息
PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 0, intent, 0);
Notification notification = new Notification.Builder(this.getApplicationContext())
.setSmallIcon(R.mipmap.app_logo)
.setContentTitle(getResources().getString(R.string.foreground_title))
.setContentText(getResources().getString(R.string.foreground_content))
.setContentIntent(pendingIntent)
.build();
return notification;
}
@Override
public void onDestroy() {
stopForeground(true);// 停止前台服务--参数:表示是否移除之前的通知
super.onDestroy();
}
}
3.小于8.0的隐藏通知的辅助服务
public class HelpService extends Service {
public class LocalBinder extends Binder {
public HelpService getService() {
return HelpService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return new LocalBinder();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}