当前位置:   article > 正文

Android设置前台服务调用startForeground()报错_requires android.permission.foreground_service

requires android.permission.foreground_service

error:Permission Denial: startForeground from pid=18524, uid=10172 requires android.permission.FOREGROUND_SERVICE

问题:缺少权限:android.permission.FOREGROUND_SERVICE

解决:在AndroidManifest.xml中加入权限

源代码:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  • 1

Android8.0服务类(myservice)的前台服务源代码:

  @Override
    public void onCreate() {
   
        super.onCreate();
        Log.d(TAG,"启动服务");
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
        NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        if
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/464475
推荐阅读
相关标签
  

闽ICP备14008679号