2016-06-09 71 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

相關問題