开发者

Android how to start a new activity WHILE screen is locked?

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.

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.

I am using the public void onNewIntent(Intent newIntent) {} method to handle when the new activity is launched.

So, the problem is, when the screen is locked and a proximity alert is fired, the Intent in开发者_JS百科 the ProximityAlertReceiver class does not get started.

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.


Maybe you should fire a notification instead (with sounds and flashing lights even if necessary) from which the activity can be launched IF wanted by the user, which would seem the right thing to do..


a couple of things are needed ...

in your reciever add the following when launching the activity

intenet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

in the onCreate of the activity add

KeyguardManager mKeyGuardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE); 
KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("name of my app");  
mLock.disableKeyguard();

this works for me in an app that is lauched at device startup

I also then mess with

Settings.System.getInt(cr,Settings.System.SCREEN_OFF_TIMEOUT,0);

but that is another story

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜