安卓14前端服务foregroundService权限问题解决办法
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>发现问题</li><li>经查资料发现:</li><li>解决方案</li><li>总结 </li></ul></div><p class="maodian"></p><h2>发现问题</h2><p>在安卓应用开发过程中,我们会使用到service,普通的service我们只需要在AndroidMainfest.xml文件中添加service类就好</p>
<div class="jb51code"><pre class="brush:xml;"><application
<service android:name=".service.MyService" />
</application></pre></div>
<p>前端服务foregroundService还需要添加</p>
<div class="jb51code"><pre class="brush:xml;"><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /></pre></div>
<p>但如果只是这样,启动前端服务还是会报错</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202509/2025091909260257.png" /></p>
<p class="maodian"></p><h2>经查资料发现:</h2>
<p><strong>Android 14(API 34)对前台服务的权限体系进行了深度重构:</strong></p>
<p><strong>1.细分权限体系</strong><br />除基础权限 <code>FOREGROUND_SERVICE</code> 外,必须根据服务类型声明对应的细分权限:</p>
<blockquote><p>除基础权限 <code>FOREGROUND_SERVICE</code> 外,必须根据服务类型声明对应的细分权限:</p>
<ul><li>数据同步服务:<code>FOREGROUND_SERVICE_DATA_SYNC</code></li><li>位置服务:<code>FOREGROUND_SERVICE_LOCATION</code></li><li>媒体播放服务:<code>FOREGROUND_SERVICE_MEDIA_PLAYBACK</code></li><li>电话服务:<code>FOREGROUND_SERVICE_PHONE_CALL</code></li></ul></blockquote>
<p><strong>2.服务类型强校验</strong></p>
<p>系统会严格验证 <code>android:foregroundServiceType</code> 属性与权限的匹配性,不匹配将抛出 <code>SecurityException</code>:</p>
<div class="jb51code"><pre class="brush:xml;">java.lang.SecurityException:
ForegroundServiceType dataSync requires android.permission.FOREGROUND_SERVICE_DATA_SYNC</pre></div>
<p><strong>3.通知权限前置要求</strong></p>
<p>从 Android 13(API 33)开始,显示通知的前台服务必须动态请求 <code>POST_NOTIFICATIONS</code> 权限</p>
<p class="maodian"></p><h2>解决方案</h2>
<p>在AndroidMainfest.xml清单文件中这样添加:</p>
<div class="jb51code"><pre class="brush:xml;"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<application
<service
android:name=".service.MyForegroundService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="dataSync">
</service>
</application></pre></div>
<p>Java代码中前端服务实现</p>
<div class="jb51code"><pre class="brush:java;">@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate:");
Toast.makeText(this, "服务已经启动", Toast.LENGTH_LONG).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id",
"通知",
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
}
//创建通知
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.baseline_music_note_24)
.setContentTitle("这是标题")
.setContentText("这是内容")
.build();
startForeground(1, notification);
}
</pre></div>
<p>运行如果发现通知栏中不存在,还需要进入设置->应用中打开app的通知权限</p>
<p>最后大家可以通过adb检查服务状态</p>
<blockquote><p>adb shell dumpsys activity services | grep "ForegroundService"</p></blockquote>
<p class="maodian"></p><h2>总结 </h2>
<p>到此这篇关于安卓14前端服务foregroundService权限问题解决办法的文章就介绍到这了,更多相关安卓14前端服务foregroundService权限内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
</div>
</div>
<!--endmain-->
頁:
[1]