- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在锁屏上显示个性化 View (实时显示一些数据)(之后用户将手机锁定在 Activity 上)。
谷歌地图和百度 map (以及其他一些我忘了名字的应用程序)已经完全实现了我想要的。
所以当我的 BroadcastReceiver 被触发时,我试图添加一个 View 。
我从 this answer 尝试了一些要点和 this one .我试过 this one也是。
在我根据许可得到一些错误之前:
permission denied for window type 2XXX
现在我没有任何错误,但我的 View 没有显示。
这是我的情况:
主要 Activity .kt:
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private val REQUEST_CODE = 10001
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkDrawOverlayPermission()
}
fun checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
else {
startService(Intent(this, LockScreenService::class.java))
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
startService(Intent(this, LockScreenService::class.java))
}
}
}
}
LockSceenService.kt:
class LockScreenService : Service() {
private var mReceiver: BroadcastReceiver? = null
private var isShowing = false
override fun onBind(intent: Intent): IBinder? {
// TODO Auto-generated method stub
return null
}
private var windowManager: WindowManager? = null
private var textview: TextView? = null
private var mView: View? = null
var params: WindowManager.LayoutParams? = null
override fun onCreate() {
super.onCreate()
windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
mView = View.inflate(baseContext, R.layout.lockscreen_view, null)
mView!!.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_VISIBLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
mView!!.visibility = View.VISIBLE
//set parameters for the textview
val flag: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
flag,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
)
params!!.gravity = Gravity.BOTTOM
//Register receiver for determining screen off and if user is present
mReceiver = LockScreenStateReceiver()
val filter = IntentFilter(Intent.ACTION_SCREEN_ON)
filter.addAction(Intent.ACTION_USER_PRESENT)
registerReceiver(mReceiver, filter)
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
return START_STICKY
}
inner class LockScreenStateReceiver : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
if (intent.action == Intent.ACTION_SCREEN_ON) {
//if screen is turn off show the textview
if (!isShowing) {
windowManager!!.addView(mView, params)
isShowing = true
}
} else if (intent.action == Intent.ACTION_USER_PRESENT) {
//Handle resuming events if user is present/screen is unlocked remove the textview immediately
if (isShowing) {
windowManager!!.removeViewImmediate(textview)
isShowing = false
}
}
}
}
override fun onDestroy() {
//unregister receiver when the service is destroy
if (mReceiver != null) {
unregisterReceiver(mReceiver)
}
//remove view if it is showing and the service is destroy
if (isShowing) {
windowManager!!.removeViewImmediate(textview)
isShowing = false
}
super.onDestroy()
}
}
list .xml:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<uses-permission android:name="android.permission.INTERNET" />
如果您有任何建议或其他方式,例如“锁定屏幕上的透明 Activity ”(?) 或只是投票,可能会有用。
感谢您的宝贵时间!
编辑:有一个 video我想要的
最佳答案
您可以在设备锁定时使用前台通知服务在锁定屏幕上显示 View ,您可以使用自定义 XML 通知布局自定义默认通知 View ,并且前台服务能够更改 View 的内容实时。
为了在锁定屏幕上显示 Activity ,您必须遵循以下方式
在您想要在锁定屏幕中显示的 Activity 中设置这些属性。
list 文件
<activity
android:name=".MainActivity"
android:screenOrientation="fullSensor"
android:showOnLockScreen="true">
在 Activity 的 onCreate
方法中添加以下行
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
如果您的设备在 Activity 打开时被锁定,它将保留在带有后退按钮的锁定屏幕中,因此您可以在需要时再次导航到锁定屏幕。
锁定屏幕上的工作示例如下:
来自 Google map 的示例通知和我们定制的通知提醒
您可以按照下面的 Gist 创建带有前台服务的自定义通知布局。
权限
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
list
<service
android:name=".MyForegroundService"
android:icon="@drawable/ic_notification"
android:label="MFS" />
服务
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
public class MyForegroundService extends Service {
private static final String TAG = "MyForegroundService";
private static final int NOTIFICATION_ID = 2999;
private static final String CHANNEL_ID = "MyForegroundService_ID";
private static final CharSequence CHANNEL_NAME = "MyForegroundService Channel";
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public MyForegroundService getService() {
return MyForegroundService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate ");
Toast.makeText(this, "The service is running", Toast.LENGTH_SHORT).show();
startForeground(NOTIFICATION_ID, createNotification("The service is running"));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return Service.START_STICKY;
}
private Notification createNotification(String message) {
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_main);
notificationLayout.setTextViewText(R.id.txtTitle, message);
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 125, notificationIntent, 0);
Bitmap payableLogo = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notification);
mBuilder.setContentTitle("My Service")
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.setLargeIcon(payableLogo)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.setAutoCancel(false)
.setCustomBigContentView(notificationLayout);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = CHANNEL_ID;
NotificationChannel channel = new NotificationChannel(channelId, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
return mBuilder.build();
}
private void showNotification(String message) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, createNotification(message));
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
通知布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="20dp"
android:paddingVertical="15dp">
<TextView
android:id="@+id/txtTitle"
style="@style/TextAppearance.Compat.Notification.Info.Media"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is from my service"
android:textColor="#fff" />
<TextView
android:id="@+id/txtResult"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Any text goes here"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
引用:https://gist.github.com/aslamanver/f32a0bb8461c250d4a945e11f6771456
关于安卓 : Displaying view over the lockscreen (like Google Maps),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60732308/
除了 Display.getOrientation() 已弃用之外,Display.getRotation() 和 Display.getOrientation() 之间还有什么区别? 是否都返回等于
我的问题 这些方法中有哪一种是专业网页设计师所偏爱的吗? Web 浏览器在绘制网站时是否首选这些方法中的任何一种? 这只是个人喜好吗? 我还缺少其他技巧吗? 注意:以上问题是关于设计多列布局 floa
我的问题 专业网页设计师是否喜欢这些方法? 网页浏览器在绘制网站时是否首选这些方法? 这只是个人喜好吗? 我还缺少其他技术吗? 注意:以上问题与设计多列布局有关 float :左; http://js
我有一些代码返回 MyTrait 类型的特征对象,这样它就可以返回几个不同结构之一。我想为 trait 对象实现 Display trait,这样我就可以打印对象,并将详细信息委托(delegate)
package polymorphism; /* * @author Rahul Tripathi */ public class OverLoadingTest { /** *
我希望 Display.timerExec(int,Runnable)与 Display.asyncExec(Runnable) 大致相同但有指定的延迟。然而,似乎Display.timerExec只
就像标题所暗示的,有什么区别吗?我当时使用的是pygame.display.flip,我在互联网上看到的是,他们使用pygame.display.update而不是使用flip。哪一个更快? 最佳答案
Sample.this.display() 和 this.display() 哪个更好? class Sample{ void display(){ System.out.println("d
当图像的 CSS 属性“显示”已被任何其他 JS 脚本/函数更改时,我想运行一些 JS 代码。有什么方法可以监视该更改并设置回调函数吗? $(this).bind.('propertychange',
在浏览 Google 字体时我注意到第一个过滤器包含这些类别: Serif Sans 衬线 展示 手写 我知道什么是 (Sans)Serif 和 Handwriting 类别(这很明显)但是显示类别过
我想知道是否可以在列标记内渲染自定义 html,这是显示表标记的一部分。 例如,我希望我的专栏里面有一些下拉列表? 使用纯 html,如下所示: ... Volvo Saab Me
display.newImage() 和 display.newImageRect() 有什么区别? 哪个更好用? 最佳答案 display.newImage() 的文档具体提到: NOTE: dis
我正在使用纯 JS 和 flexbox 为我的元素创建网格。 元素的某些部分在页面加载时被显示隐藏:无,但单击按钮后它应该在不显示和阻止之间切换。 可悲的是,这完全破坏了 display: flex
我目前正在参加 HTML/CSS 类(class),这本书推荐我使用.desktop {display:none;}/.mobile {display:inline;} 以及div class="de
这个问题在这里已经有了答案: Css transition from display none to display block, navigation with subnav [duplicat
我理解 style="display: none" 隐藏一个 HTML 元素,而 style="display: block" 显示一个 block 级 HTML 元素。 我看到一些使用 style=
设置控件的样式 display: none 和 display: block 有什么区别? 最佳答案 display 属性定义了某个 HTML 元素应该如何显示。 Display block 和 no
这个问题已经有答案了: Javascript AND operator within assignment (7 个回答) 已关闭 4 年前。 假设我只想在 this.state.display 为
我不确定如何命名这个问题,因为我是 Rust 新手,所以请随意提出修改建议。 我有两个结构。一个是 Job 结构,其中包含一些数字,例如作业需要多长时间等。另一个是 JobSequence,其中包含
我不确定如何命名这个问题,因为我是 Rust 新手,所以请随意提出修改建议。 我有两个结构。一个是 Job 结构,其中包含一些数字,例如作业需要多长时间等。另一个是 JobSequence,其中包含
我是一名优秀的程序员,十分优秀!