微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:在LockScreen上覆盖TextView

我试图覆盖一个TextView在LockScreen顶部(类似于Android Overlays的时间).

注意:我不想旁路锁屏,但只是画在它的顶部(不干扰任何触摸事件).

我试过使用以下标志(在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);

并应用以下主题(到具体活动):

<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>

活动清单声明(其膨胀overlay_activity.xml)

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

解决方法

由于您希望从应用程序活动中显示内容,您可以使用Service和WindowManager,与Facebook Messenger和其他浮动Windows应用程序的工作方式相同;)

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>

不要忘了在您的活动onCreate()上启动服务

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

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

}

TextView显示锁屏

解锁时删除TextView

原文地址:https://www.jb51.cc/android/311635.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐