Android:在锁屏上覆盖 TextView

标签 android textview overlay android-windowmanager

Concept

我试图在锁定屏幕上覆盖一个 TextView(类似于 Android 覆盖时间的方式)。

注意:我不想绕过锁屏,只是在它上面画画(不干扰任何触摸事件)。

我尝试使用以下标志(在 onCreate 中):

getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    getWindow().addFlags(PixelFormat.TRANSLUCENT);

并应用以下主题(针对特定 Activity ):

   <style name="Transparent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:backgroundDimEnabled">false</item>
 <item name="android:windowIsFloating">true</item>   
</style>

但这利用了锁屏的顶部隐藏锁屏并禁用所有触摸事件。

编辑:activity_overlay.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.coco.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:layout_alignParentBottom="true"
    android:text="TextView" />

</RelativeLayout>

Activity 的 list 声明(膨胀 overlay_activity.xml)

      <activity
        android:name=".DisplayActivity"
        android:label="@string/app_name"
        android:theme="@style/Transparent" />

最佳答案

由于您想显示应用程序 Activity 之外的内容,您可以为此使用服务和 WindowManager,就像 Facebook Messenger 和其他 float 窗口应用程序的工作方式一样;)

LockScreenTextService.class

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Created on 2/20/2016.
 */
public class LockScreenTextService extends Service {

    private BroadcastReceiver mReceiver;
    private boolean isShowing = false;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private WindowManager windowManager;
    private TextView textview;
    WindowManager.LayoutParams params;

    @Override
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);

        //add textview and its properties
        textview = new TextView(this);
        textview.setText("Hello There!");
        textview.setTextColor(ContextCompat.getColor(this, android.R.color.white));
        textview.setTextSize(32f);

        //set parameters for the textview
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.BOTTOM;

        //Register receiver for determining screen off and if user is present
        mReceiver = new LockScreenStateReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);

        registerReceiver(mReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    public class LockScreenStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                //if screen is turn off show the textview
                if (!isShowing) {
                    windowManager.addView(textview, params);
                    isShowing = true;
                }
            }

            else if(intent.getAction().equals(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
    public void 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();
    }

}

并在 AndroidManifest.xml 上添加必要的权限并添加服务

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textonlockscreenusingservice">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".LockScreenTextService" /> //this

    </application>

</manifest>

不要忘记在您的 Activity 的 onCreate() 上启动服务

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, LockScreenTextService.class);
    startService(intent);

}

在锁屏上显示 TextView TextView showing on lockscreen

解锁时移除 TextView TextView removed when unlocked

关于Android:在锁屏上覆盖 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327328/

相关文章:

android - 主页和后退按钮在 android 模拟器像素 2 和像素 3 api 中不起作用

java - 如何传递按钮 Intent 以在另一个 Activity 中更改颜色?

ios - Swift 1/2 UITextView 建议通过标签 "@"或 "#"

android - 具有背景颜色和行间距的 TextView

android - flutter 错误无法确定任务 ':app:compileDebugJavaWithJavac'的依赖关系

Android VideoView 无法播放

android - TextView setVisibility(View.GONE) 没有删除我的 CustomTextView

android - 如何从 Overlay onTap 方法将按下的位置返回到 MapActivity

swift - map View 上的倒圆圈叠加(MapKit、MKOverlay、Swift)

css - 图像悬停覆盖 : handle the opacity of nested element