2017-07-14 38 views
6

我試圖做一個系統覆蓋窗口,它也會出現在鎖屏上。我確實在版本6.0上做到了這一點,但它不適用於版本7.0和8.0。第一次運行時,我被轉移到我的應用程序的覆蓋設置(如Android M &起,預計),我允許它。但現在它不斷崩潰,因爲我試圖從應用程序啓動服務。 我想這是有關這個問題的所有事情,但沒有什麼幫助我解決我每次嘗試啓動該服務時它yet.I'm收到此錯誤: -

FATAL EXCEPTION: main 
Process: com.example.sumuix.lockdown, PID: 5128 
java.lang.RuntimeException: Unable to create service com.example.sumuix.lockdown.MyService:android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for window type 2010 
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3414) 
at android.app.ActivityThread.-wrap4(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6540) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Caused by: android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for window type 2010 
at android.view.ViewRootImpl.setView(ViewRootImpl.java:788) 
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92) 
    at com.example.sumuix.lockdown.MyService.onCreate(MyService.java:64) 
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404) 
at android.app.ActivityThread.-wrap4(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6540) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

這裏是我的代碼:-(MyService.java)

@Override 
public void onCreate() { 
    super.onCreate(); 
      HeadView = LayoutInflater.from(this).inflate(R.layout.overlay_head,null); 
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.WRAP_CONTENT, 
      WindowManager.LayoutParams.WRAP_CONTENT, 
      WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
      WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
      PixelFormat.TRANSLUCENT); 
    params.gravity = Gravity.TOP | Gravity.LEFT; 
    params.x = 0; 
    params.y = 100; 
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
    mWindowManager.addView(HeadView, params); //I’m getting error here...(MyService.java:64)from error log 

    ImageView closeButton = (ImageView) HeadView.findViewById(R.id.close_btn); 
    closeButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      stopSelf(); 
     } 
    }); 

    final ImageView chatHeadImage = (ImageView) HeadView.findViewById(R.id.head); 
    chatHeadImage.setOnTouchListener(new View.OnTouchListener() { 
//Code for OnTouch. 
     } 
    }); 
} 

我的MainActivity,當我打電話給上述服務: - (MyActivity.java)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    back=(Button) findViewById(R.id.jump); 
final Intent intentService=new Intent(MainActivity.this, MyService.class); 
    if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(MainActivity.this)) { 

     //If the draw over permission is not available open the settings screen 
     //to grant the permission. 
     Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 
       Uri.parse("package:" + getPackageName())); 
     startActivityForResult(intent,1234); 
    }else { 
     back.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       startService(intentService); 
       finish(); 
      } 
     }); 
    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1234) { 

     //Check if the permission is granted or not. 
     if (Build.VERSION.SDK_INT >= 23 && Settings.canDrawOverlays(MainActivity.this)) { 
      back.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        startService(new Intent(MainActivity.this, MyService.class)); 
        finish(); 
       } 
      }); 
     } else { //Permission is not available 
      Toast.makeText(this, 
        "Draw over other app permission not available. Closing the application", 
        Toast.LENGTH_SHORT).show(); 

      finish(); 
     } 
    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

在manifeast: -

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

爲MyService佈局XML: -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/chat_head_root" 
android:layout_width="65dp" 
android:layout_height="wrap_content" 
android:background="@android:color/black" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/head" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginTop="8dp" 
    android:background="@android:drawable/ic_menu_info_details" 
    tools:ignore="ContentDescription" /> 

<ImageView 
    android:id="@+id/close_btn" 
    android:layout_width="26dp" 
    android:layout_height="26dp" 
    android:layout_marginLeft="40dp" 
    android:background="@android:drawable/ic_menu_close_clear_cancel" 
    tools:ignore="ContentDescription" /> 
</RelativeLayout> 

我想幾乎我發現,而在internet.I搜索剛剛開始使用Android所以,如果我做的任何微小的失誤,那麼請讓每一件事情我知道它。提前感謝。

回答

7

對於android 8.0,您必須使用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY。錯誤覆蓋,電話覆蓋和類似的不再工作。

查看更多在這裏:https://developer.android.com/about/versions/oreo/android-8.0-changes.html#o-apps

不知道爲什麼你在Android 7的問題,雖然,從未有過他們自己......

旁註

不要使用startService瞭如果您的服務是前臺服務,如果您的服務是前臺服務,則使用startForegroundServiceContextCompat版本,以避免在啓動後或其他任何地方從後臺啓動覆蓋服務時可能出現的問題...

相關問題