Android12(SDK31)中出现android.app.ForegroundServiceStartNotAllowedException问题
创始人
2024-09-30 23:00:29
0

由于Android 12中对前台服务的限制更加严格,因此需要进行特殊设置。

具体代码如下:

在 AndroidManifest.xml 文件中,需要添加以下代码:

其中,foregroundServiceType可以选择location、dataSync、mediaPlayback这三个其中的一个。根据实际需求来选择,其中"location"是基于位置的服务,"dataSync"是基于数据的同步服务,而"mediaPlayback"是基于媒体的播放服务。

在启动前台服务时,需要使用startForegroundService()方法,然后调用startForeground()方法为服务设置通知图标和文本。

示例代码如下:

Intent serviceIntent = new Intent(this, MyForegroundService.class); ContextCompat.startForegroundService(this, serviceIntent);

MyForegroundService.java:

public class MyForegroundService extends Service {

...

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle(getText(R.string.notification_title))
            .setContentText(getText(R.string.notification_message))
            .setSmallIcon(R.drawable.ic_notification)
            .build();

    startForeground(1, notification);

    return START_STICKY;
}

}

相关内容

热门资讯

WePoKe透明挂!德扑快速计... WePoKe透明挂!德扑快速计算胜率(透视)本然是有挂(详细辅助AA德州教程)1、超多福利:超高返利...
透视规律!aapkoer德州辅... 透视规律!aapkoer德州辅助挂下载,wepoker养号规律(详细辅助安装教程)一、aapkoer...
德州之星外挂!wpk微扑克真的... 德州之星外挂!wpk微扑克真的有辅助(透视)从前是真的有挂(详细辅助黑科技教程)1、不需要AI权限,...
透视实锤!wpk辅助器安装,w... 透视实锤!wpk辅助器安装,wpk俱乐部是做什么的(详细辅助透明挂教程)1、wpk辅助器安装系统规律...
WePoKe透明挂!wpk微扑... WePoKe透明挂!wpk微扑克辅助ai(透视)其实有挂(详细辅助wepoke教程)(1)WePoK...
透视神器!wpk透视辅助靠谱吗... 透视神器!wpk透视辅助靠谱吗,wepoker国外版透视(详细辅助AI教程)1、这是跨平台的wpk透...
wepoke有挂!nzt德州辅... wepoke有挂!nzt德州辅助(透视)一直存在有挂(详细辅助专业教程)1、金币登录送、破产送、升级...
透视插件!wepoker透视脚... 透视插件!wepoker透视脚本网页,wepoker免费辅助器(详细辅助透牌教程)1、金币登录送、破...
微扑克游戏辅助器!红龙扑克模拟... 微扑克游戏辅助器!红龙扑克模拟器(透视)原生真的是有挂(详细辅助技巧教程)(1)微扑克游戏辅助器!红...
透视攻略!wpk辅助插件,we... 透视攻略!wpk辅助插件,wepoker轻量版透视(详细辅助2025新版教程);在进入wpk辅助插件...