android 8.0后新加的后台运行限制权限能在代码中设置吗?怎么设置,请指教.
在android 有一个配置文件AndroidManifest.xml,里面 可以系统权限
https://www.jianshu.com/p/9c7723036b73
很简单,使用startForeground将后台服务改为前台服务就可以了。
private void makeForegroundService() {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel;
String channelId;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
channelId = "channel";
channel = new NotificationChannel(channelId, "MainActivity", NotificationManager.IMPORTANCE_NONE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
} else {
channelId = null;
}
Notification notification = new Notification.Builder(this, channelId).setContentTitle("Title").setContentText("ContentMessage").setContentIntent(pendingIntent).build();
startForeground(SYSTEM_FIXED_SERVICE_ID, notification);
}