2016-06-09 48 views
-1

尝试启动服务时发生错误我有一个在主类中启动服务的Android应用程序。所以我有这个错误当我尝试从主活动

06-09 09:34:00.880 26760-26760/it.eresult.decipher E/AndroidRuntime: FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=it.eresult.decipher/.service.CriticalInformationService }: android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for this window type 
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376) 
    at android.app.ActivityThread.access$1900(ActivityThread.java:123) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4424) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for this window type 
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:537) 
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301) 
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
    at it.eresult.decipher.service.CriticalInformationService.onStartCommand(CriticalInformationService.java:37) 
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359) 
    at android.app.ActivityThread.access$1900(ActivityThread.java:123)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)  
    at android.os.Handler.dispatchMessage(Handler.java:99)  
    at android.os.Looper.loop(Looper.java:137)  
    at android.app.ActivityThread.main(ActivityThread.java:4424)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:511)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)  
    at dalvik.system.NativeStart.main(Native Method)  

这是我AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="it.eresult.decipher"> 


    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     android:background="@color/colorToolBar"> 


     <activity 
      android:name=".activity.MainActivity"> 

     </activity> 

     <activity 
      android:name=".activity.LoginActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name=".service.CriticalInformationService" 
      android:label="Critical Information Service" 
      > 
     </service> 

     <uses-permission 
      android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    </application> 

</manifest> 

有了这个代码,我尝试启动服务:

startService(new Intent(this, CriticalInformationService.class)); 

那么,这是服务:

public class CriticalInformationService extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     //TODO do something useful 
     WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
     LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
     View mView = mInflater.inflate(R.layout.settings_activity, null); 

     WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0, 
       WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
       WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
         | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
         | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */, 
       PixelFormat.RGBA_8888); 

     mWindowManager.addView(mView, mLayoutParams); 
     return Service.START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     //TODO for communication return IBinder implementation 
     return null; 
    } 
} 

编辑

这是启动在MainActivity服务

public class LoginActivity extends AppCompatActivity { 
    private static final int REQUEST_RUNTIME_PERMISSION = 1; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     checkPremission(); 
     //startService(new Intent(this, CriticalInformationService.class)); 
    } 

    void checkPremission() { 
     //select which permission you want 
     final String permission = Manifest.permission.SYSTEM_ALERT_WINDOW; 
     // if in fragment use getActivity() 
     if (ContextCompat.checkSelfPermission(LoginActivity.this, permission) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, permission)) { 

      } else { 
       ActivityCompat.requestPermissions(LoginActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION); 
      } 
     } else { 
      // you have permission go ahead launch service 

     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode) { 
      case REQUEST_RUNTIME_PERMISSION: 
       final int numOfRequest = grantResults.length; 
       final boolean isGranted = numOfRequest == 1 
         && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1]; 
       if (isGranted) { 
        // you have permission go ahead launch service 
       }else{ 
        // you dont have permission show toast 
       } 
       break; 
      default: 
       super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 
    } 

} 

我怎样才能解决这个问题?

+1

看到这个职位http://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t#answer-34061521可能会帮助 –

+0

我已阅读该帖子,但我不明白我该如何解决我的错误 – bircastri

+0

已经问过这个'android.permission.SYSTEM_ALERT_WINDOW'的权限在运行时在API 23 –

回答

1
public class LoginActivity extends AppCompatActivity { 
    private static final int REQUEST_RUNTIME_PERMISSION = 1; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     checkPremission(); 
     //startService(new Intent(this, CriticalInformationService.class)); 
    } 

    void checkPremission() { 
     //select which permission you want 
     final String permission = Manifest.permission.SYSTEM_ALERT_WINDOW; 
     // if in fragment use getActivity() 
     if (ContextCompat.checkSelfPermission(LoginActivity.this, permission) 
       != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this, permission)) { 

      } else { 
       ActivityCompat.requestPermissions(LoginActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION); 
      } 
     } else { 
      // you have permission go ahead launch service 
      startService(new Intent(this, CriticalInformationService.class)); 
     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     switch (requestCode) { 
      case REQUEST_RUNTIME_PERMISSION: 
       final int numOfRequest = grantResults.length; 
       final boolean isGranted = numOfRequest == 1 
         && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1]; 
       if (isGranted) { 
        // you have permission go ahead launch service 
        startService(new Intent(this, CriticalInformationService.class)); 
       }else{ 
        // you dont have permission show toast 
       } 
       break; 
      default: 
       super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 
    } 

} 
+0

我在MinActivity上复制并粘贴这段代码的权利? – bircastri

+0

你在这里调用'startService(new Intent(this,CriticalInformationService.class));'现在调用'checkSelect()' –

+0

好吧,我插入checkPermission()而不是startService ....对吧? – bircastri

相关问题