问题描述
我看过关于此事的线程,但全部都是关于发起一个活动,当屏幕被锁定或当它被解锁。但是,我需要为我的节目,无论启动屏幕的一个新的活动被锁定或没有。
I have read threads on the matter but all of them were regarding launching an activity WHEN the screen is locked or when it is unlocked. However, I need for my program to launch a new activity regardless of the screen being locked or not.
我用GPS和感应警报检查时,目标已经达到。我的活动注册了一个ProximityAlertReceiver这样:
I am using gps and proximity alerts to check when a destination has been reached.My activity registers a ProximityAlertReceiver such that:
private class ProximityAlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
System.out.println("You have entered the proximity area");
} else {
System.out.println("You have exited the proximity area");
}
Bundle bundle = intent.getExtras();
int status = bundle.getInt("status");
Intent i = new Intent();
i.setClass(context, MEcheScreen.class);
Bundle bundle1 = new Bundle();
bundle1.putInt("status", status);
i.putExtras(bundle1);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
}
}
所以,当我接近火灾警报,新的活动将启动。
So, when I a proximity alert fires, a new activity will be started.
我现在用的是公共无效onNewIntent(意向newIntent){}
的方法来处理时,新的活动推出。
I am using the public void onNewIntent(Intent newIntent) {}
method to handle when the new activity is launched.
所以,问题是,当屏幕被锁定和接近警报被触发,在ProximityAlertReceiver类的意图不上手。
So, the problem is, when the screen is locked and a proximity alert is fired, the Intent in the ProximityAlertReceiver class does not get started.
我试图用keyguardmanager禁用键盘锁。然而,在它已被禁用,它返回到程序的主界面,但活动仍没有启动,直到我preSS按钮或点击屏幕。
I tried to use the keyguardmanager to disable the keyguard. However, after it has been disabled, it returns to the main screen of the program, but the activity is still not started until I press a button or tap the screen.
推荐答案
需要几件事情...
在你的reciever开展活动时,添加以下
in your reciever add the following when launching the activity
intenet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
在活动的onCreate添加
in the onCreate of the activity add
KeyguardManager mKeyGuardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("name of my app");
mLock.disableKeyguard();
这对我的作品在被lauched在设备启动一个应用程序
this works for me in an app that is lauched at device startup
我还那么乱用
Settings.System.getInt(cr,Settings.System.SCREEN_OFF_TIMEOUT,0);
但这是另外一个故事了。
but that is another story
这篇关于如何的Android开始一个新的活动,而屏幕被锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!