需要在Android的解锁屏幕之前运行我的应用程序

时间:2013-07-17 11:51:00

标签: android broadcastreceiver

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

我的BroadcastReceiver

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

}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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);

答案 2 :(得分:0)

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

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

我知道有一些黑客的方法,但它们不起作用 对于每台设备,在HTC和Samsumg上都有点破解,没有app应该 能够锁定用户。

史蒂夫

相关问题