窗口管理器在我的设备上没有显示任何内容



我一直在尝试使用窗口管理器来显示其他应用程序的视图,但每当我点击启动时,我的设备上都不会发生任何事情。我看过其他指南和教程,但由于某种原因,我无法将其显示在我的设备上(Pophone F1 Android Pie)。当我将参数类型更改为type_PHONE并在较低的API上运行它时,它运行得很好。当我将其设置为TYPE_APPLICATION OVERLAY并在更高的API上运行时,它不起作用。不确定我做错了什么,如果有任何帮助,我将不胜感激。

Manifest.xml

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" />
</application>

activity_main.xml

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<Button
android:id="@+id/notify_me"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service" />
</LinearLayout>

popup.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<TextView
android:id="@+id/popupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS TEXT FOR TESTING PURPOSES." />
</LinearLayout>

主要活动.java


public class MainActivity extends AppCompatActivity {
private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
} else {
initializeView();
}
}
private void initializeView() {
findViewById(R.id.notify_me).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, MyService.class));
finish();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
if (resultCode == RESULT_OK) {
initializeView();
} else {
Toast.makeText(this,
"Draw over other app permission not available. Closing the application",
Toast.LENGTH_SHORT).show();
finish();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}

MyService.java

public class MyService extends Service {
private WindowManager wm;
private View view;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
view=LayoutInflater.from(this).inflate(R.layout.popup,null);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.format = PixelFormat.TRANSPARENT;
params.gravity = Gravity.CENTER;
params.x=0;
params.y=0;
wm.addView(view, params);
}
@Override
public void onDestroy() {
super.onDestroy();
wm.removeView(view);
}
}

当您为结果启动活动时,您将0作为结果代码传递,而在回调onActivityResulty时,您正在根据code_DRAW_OVER_OTHER_APP_PERMISSION检查结果代码。这没有道理。请使用0更改此项,或在调用startActivityForResult()方法时传递CODE_DRAW_OVER_OTHER_APP_PERMISSION。希望这是有道理的。如果你还有问题,请告诉我。

最新更新