6

How I can start my activity even when phone is in locked mode so I can access my application?

I want to ring an alarm first using ringAlarm() then goes to another activity that will display alertdialog to stop the alarm.

It works fine if phone is active, but when the phone is locked, it just alarms continuously and by the time i unlock the phone, the stop alarm button doesn't activate. Please help me. Thanks for help in advance.

Here is my code:

public class EAlarmReceiver extends BroadcastReceiver {
public static String sender;
public static String sms = "";
public static Ringtone r;
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras(); 
    Object[] pdusObj = (Object[]) bundle.get("pdus"); 
    SmsMessage[] messages = new SmsMessage[pdusObj.length]; 
    for (int i = 0; i<pdusObj.length; i++) 
    { 
            messages[i] = SmsMessage.createFromPdu ((byte[]) 
            pdusObj[i]); 
            sender = messages[i].getOriginatingAddress();
            sms = messages[i].getMessageBody();//save sms to string
    } 

    for (SmsMessage msg : messages) {
        if (msg.getMessageBody().contains("alert")) {

            Intent openStopAlarm = new Intent("proj.receiver.STOPALARM");
            openStopAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_HISTORY);
            ringAlarm(context);
            context.startActivity(openStopAlarm);
            abortBroadcast();
        }//end if
    }//end for
}// end onreceive

//ring the alarm
public void ringAlarm(Context context)
{
     Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
     if(alert == null){
         // alert is null, using backup
         alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
         if(alert == null){  // 
             // alert backup is null, using 2nd backup
             alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
         }
     }
     r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
     AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
     int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
     int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
     audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
     audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
     audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
     r.play();
        Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
}
//end ringAlarm()
}

I've seen a code on how to do it but I didn't know how to use it. so I'm still having the same problem. Here's the code.

public class DismissLock extends Activity {

PowerManager pm;
WakeLock wl;
KeyguardManager km;
KeyguardLock kl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.i("INFO", "onCreate() in DismissLock");
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    kl= km.newKeyguardLock("INFO");
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
    wl.acquire(); //wake up the screen
    kl.disableKeyguard();// dismiss the keyguard

    setContentView(R.layout.main);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release(); 
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    wl.acquire();//must call this!
}

}
4

2 回答 2

8

您只需要设置一些额外的窗口标志:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
        + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

来源:默认锁定屏幕上的 Android 活动

于 2013-01-28T02:55:43.163 回答
2
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
    + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
    + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
    + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

将上面的代码粘贴到您要在锁定屏幕上执行的那个活动中。将代码放入 oncreate();

于 2015-12-21T18:14:09.053 回答