Android 8.0以上启动服务ANR问题解决

ServiceCompat是一个抽象类,扩展了Android的Service。它在创建时和启动命令时检查系统版本,适配Android8.0及以上版本的通知渠道。在适配版本中,创建并设置了一个低重要性的NotificationChannel,并使用startForeground()显示通知。此外,还提供了获取通知内容和构建通知的方法,子类可以重写这些方法以自定义通知。
摘要由CSDN通过智能技术生成

自己的service可继承下面的ServiceCompat类。

public abstract class ServiceCompat extends Service {

@Override

public void onCreate() {

super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//适配安卓8.0

String channelId = getChannelId() + "";

String channelName = getChannelName();

NotificationChannel channel = new NotificationChannel(channelId, channelName,

NotificationManager.IMPORTANCE_MIN);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

manager.createNotificationChannel(channel);

startForeground(getChannelId(), getNotification());

//

// new Handler().postDelayed(new Runnable() {

// @Override

// public void run() {

// stopForeground(true);

// }

// }, 300);

}

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

// //适配安卓8.0

// String channelId = getChannelId() + "";

// String channelName = getChannelName();

// NotificationChannel channel = new NotificationChannel(channelId, channelName,

// NotificationManager.IMPORTANCE_MIN);

// NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// manager.createNotificationChannel(channel);

//

// startForeground(getChannelId(), getNotification());

//

// new Handler().postDelayed(new Runnable() {

// @Override

// public void run() {

// stopForeground(true);

// }

// }, 300);

// }

return START_STICKY;

}

/**

* Notification channelName

*

*/

protected abstract String getChannelName();

/**

* Notification channelId,must not be 0

*

*/

protected abstract int getChannelId();

/**

* Default content for notification , subclasses can be overwritten and returned

*/

public String getNotificationContent(){

return "";

}

/**

* Displayed notifications, subclasses can be overwritten and returned

*/

public Notification getNotification() {

return createNormalNotification(getNotificationContent());

}

protected Notification createNormalNotification(String content) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getChannelId() + "");

if (TextUtils.isEmpty(content)) {

return builder.build();

}

builder.setContentTitle(getString(R.string.app_name))

.setContentText(content)

.setWhen(System.currentTimeMillis())

//.setSmallIcon(getSmallIcon())

// .setLargeIcon(getLargeIcon())

.build();

return builder.build();

}

/**

* Large icon for notification , subclasses can be overwritten and returned

*/

// public Bitmap getLargeIcon() {

// return BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

// }

/**

* Small icon for notification , subclasses can be overwritten and returned

*/

// // public int getSmallIcon() {

// return R.mipmap.ic_launcher;

// }

public static void startService(Context context, Intent intent){

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//适配安卓8.0

context.startForegroundService(intent);

}else{

context.startService(intent);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值