我试图制作一个系统覆盖窗口,它也会出现在锁屏上。我确实设法在版本6.0上这样做,但它不适用于版本7.0和8.0。在第一次运行时,我被转移到我的应用程序的覆盖设置(正如Android M& on预期的那样)并且我允许它。但是现在当我试图从应用程序启动服务时它不断崩溃。 我尝试了所有与此问题相关的事情,但没有任何帮助我解决它。我每次尝试启动服务时都会收到此错误: -
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 android.view.ViewRootImpl$W@61b864 -- 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 android.view.ViewRootImpl$W@61b864 -- 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.
}
});
}
我的主要活动,我在哪里调用上述服务: - (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);
}
}
在表现中: -
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
MyService Layout 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>
我在网上搜索时发现了几乎所有的东西。我刚开始使用Android,如果我做任何小错误,请告诉我。谢谢。
答案 0 :(得分:9)
对于Android 8.0,你必须使用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
。错误覆盖,手机覆盖和类似功能不再有效。
在此处查看更多内容:Bootstrap Writing In Columns (like a newspaper)
不知道你为什么在Android 7上遇到问题,从来没有自己拥有它们......
<强>旁注强>
如果您的服务是前台服务,请不要再使用startService
,如果您的服务是前台服务,请使用startForegroundService
或ContextCompat
版本以避免可能出现的问题想要在启动后或在后台的任何其他地方启动覆盖服务......