我一直试图使用窗口管理器来显示其他应用程序的视图,但是每当我点击开始时,我的设备上就什么也没有发生。 我看过其他指南和教程,但是由于某种原因,我无法在设备(Pocophone 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>
MainActivity.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 :(得分:0)
当您为结果启动活动时,您在回叫 onActivityResulty 时正在传递0作为结果代码,您正在对照 CODE_DRAW_OVER_OTHER_APP_PERMISSION 检查结果代码。那没有意义。请在调用 startActivityForResult()方法时使用 0 进行更改或通过 CODE_DRAW_OVER_OTHER_APP_PERMISSION 。希望这是有道理的。如果仍然有问题,请告诉我。