有没有一种使用窗口管理器冻结旧设备(特别是API 17)屏幕的方法?

时间:2018-12-20 11:18:00

标签: android

3天前,我尝试使用WindowManager为旧设备创建冻结屏幕,但从来没有用。

有关我的应用的详细信息

我想要当我单击按钮时得到以下信息:

  1. 关闭我的应用程序(进入主屏幕)。
  2. 在主屏幕上显示我的自定义布局。
  3. 我希望何时单击屏幕上的任何位置时都无法响应我想只响应自定义布局,例如,单击相机,图库或其他我不会响应的应用程序时,但是单击我的任何按钮或图像时等我想要自定义的布局。


我尝试通过下面的代码

单击按钮时

Intent intent = new Intent(this, LockScreenActivity.class);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(getResources().getString(R.string.notification_content_text))
                .setSmallIcon(R.drawable.notification_small_icon)
                .setContentIntent(PendingIntent.getActivity(this, 0, intent, 0))
                .setAutoCancel(true)
                .setOngoing(true)
                .build();
notificationManager.notify(0, notification);

LockScreenActivity类

public class LockScreenActivity extends AppCompatActivity {

    @BindView(R.id.image_unlock_screen)
    ImageView image_unlock_screen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock_screen);
        ButterKnife.bind(LockScreenActivity.this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(new Intent(getBaseContext(), WindowManagerService.class));
        } else {
            startService(new Intent(getBaseContext(), WindowManagerService.class));
        }
        moveTaskToBack(true);
    }

}

WindowManagerService类

public class WindowManagerService extends Service {

    WindowManager windowManager;
    RelativeLayout relativeLayout;
    WindowManager.LayoutParams params;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        int LAYOUT_FLAG = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_TOAST;
        windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        relativeLayout = (RelativeLayout) inflater.inflate(R.layout.activity_lock_screen, null);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);
        windowManager.addView(relativeLayout, params);
    }
}

LockScreen布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lock_screen_background"
    tools:context=".LockScreen.LockScreenActivity">

    <ImageView
        android:id="@+id/image_unlock_screen"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="50dp"
        android:src="@drawable/finger_icon" />

</RelativeLayout>

权限

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

注意:在我将主题发布到这里之前,我在StackOverflow网站上看到了一些建议中的帖子,并通过Google搜索了三天关于我的请求的内容,但是找不到与我的请求相似的主题。

0 个答案:

没有答案