2

当用户点击解锁屏幕时,我需要运行我的应用程序。我在 BroadcastReceiver 中使用了 ACTION_USER_PRESENT Intent 进行检查。我使用了以下代码。我需要在解锁屏幕之前显示我的应用程序。但是在我解锁屏幕后我的应用程序是可见的。任何帮助将不胜感激。

我的广播接收器

package com.progressindicator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;

public class Receive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction() != null) {
            if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {

                Intent s = new Intent(context, ProgressIndicator.class);
                s.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                // s.setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

                s.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                context.startActivity(s);
            }
        }
    }
}

显现

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.progressindicator.ProgressIndicator"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver android:name=".Receive" >
            <intent-filter
                android:enabled="true"
                android:exported="false" >
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我的活动:

package com.progressindicator;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ProgressIndicator extends Activity {
    ImageView loading;
    AnimationDrawable loadAnimation;
    private Window wind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_progress_indicator);
    //  requestWindowFeature(Window.FEATURE_NO_TITLE);
    //  requestWindowFeature(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//      wind = this.getWindow();
//      wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
//      wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//      wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

        getWindow().addFlags(

                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 

            );
        LinearLayout main = new LinearLayout(this);
        main.setOrientation(LinearLayout.VERTICAL);
        setContentView(main);

        loading = new ImageView(this);
        loading.setImageResource(R.drawable.loading);
        loadAnimation = (AnimationDrawable)loading.getDrawable();
        main.addView(loading);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus){

    loadAnimation.start();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.progress_indicator, menu);
        return true;
    }

}
4

3 回答 3

1

尝试添加 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 以启动您的活动,只要您的活动位于所有其他窗口之上,这将暂时禁用键盘锁/键盘锁。

于 2015-07-27T21:25:17.387 回答
0

前段时间我将它用于一个项目

// set flags for staying on and lockscreen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
       | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

此外,我会检查您的 Intent 参数以确保您创建一个新的 Activity 并调用 onCreate 。

如果您想禁用锁定屏幕,您需要成为设备管理员。

编辑:

ACTION_USER_PRESENT 在用户解锁屏幕后发送,这就是为什么它不适用于在锁屏顶部显示屏幕的原因。

您应该更改为 ACTION_SCREEN_ON。

编辑2:在我拥有的清单上android:launchMode="singleTask",并在意图中我调用活动:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
于 2013-07-17T12:16:42.280 回答
0

您需要添加WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED到您的窗口标志,如Activity.getWindow().addFlags(...). 我建议另外使用FLAG_WATCH_OUTSIDE_TOUCH它并在你的 onCreate 生命周期方法中调用它。

到“安全漏洞”话题。作为应用程序开发人员,您可以在安全锁屏出现之前弹出是正确的,但是简单的主页按钮点击会暂停您的活动(甚至是系统警报)并显示安全防护。

我知道有一些 hacky 方法可以解决这个问题,但它们并不适用于所有设备,在 HTC 和 Samsumg 上有些损坏,并且没有应用程序应该能够锁定用户。

史蒂夫

于 2015-08-17T18:41:05.123 回答