yuejiang10516
yuejiang10516
采纳率50%
2018-10-30 03:38 阅读 3.9k

android 8.0后台运行限制

10

android 8.0后新加的后台运行限制权限能在代码中设置吗?怎么设置,请指教.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • qq_36423978 徐小涛_nice 2018-10-30 05:03

    在android 有一个配置文件AndroidManifest.xml,里面 可以系统权限

    点赞 评论 复制链接分享
  • ziyejinwei1994 ziyejinwei1994 2018-10-30 12:40
    点赞 评论 复制链接分享
  • haijie_liu haijie_liu 2018-12-27 05:18

    很简单,使用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);
        }
    
    点赞 评论 复制链接分享

相关推荐