Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start an Activity even when Android phone is in locked mode / on lock screen

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

}
like image 277
Tokuchi Toua Avatar asked Jan 28 '13 02:01

Tokuchi Toua


People also ask

How do I make you work even when my phone is locked?

On your Android phone or tablet, say "Hey Google, open Assistant settings." Or, go to Assistant settings. Under "All settings," tap Lock screen. Turn Allow Assistant on lock screen on or off.

How do I show activity on my Android lock screen?

To enable it you have to go to the app configuration -> Other permissions -> Show On Lock screen.

What is lock task mode?

Lock task mode settings allows the users to disable all the major system UI features such as home button, notifications, recent apps button, and global actions. Enabling lock task mode on the Android device transforms the device into an immersive kiosk, with only the whitelisted apps active on the device.


1 Answers

You just need to set a few additional window flags:

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

Source: Android activity over default lock screen

like image 105
Oleg Vaskevich Avatar answered Sep 18 '22 17:09

Oleg Vaskevich