当用户点击解锁屏幕时,我需要运行我的应用程序。我在 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;
}
}