android 前台服务自定义布局不显示_android 开启前台服务,并隐藏8.0以下的通知...

这篇博客介绍了如何在Android中实现自定义前台服务,特别是在API 8.0以上如何处理前台服务通知的显示。内容包括通过启动服务、设置不同版本系统的通知处理方式,以及创建辅助服务来隐藏8.0以下版本的通知。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值