3天前,我尝试使用WindowManager
为旧设备创建冻结屏幕,但从来没有用。
有关我的应用的详细信息
我想要当我单击按钮时得到以下信息:
我尝试通过下面的代码
单击按钮时
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搜索了三天关于我的请求的内容,但是找不到与我的请求相似的主题。