Reputation: 11
I'm trying to code an alarm (it is my first app). I'm sure it is very simple to perfom, but I am stuck. I call activity in exact time that set in TimePickerDialog
. I use getActivity()
, so I do it without BroadcastReceiver
. It's not excluded that I chose poorly, but unfortunately, it was only the one that I found.
When the time comes 'MainActivity.class' opens, but if the screen is locked, it happens, but the screen stays off. I tryid to use WAKE_LOCK
, but I suppose I did it in wrong way, because I had errors in POWER_MANAGER
line among others.
private void setAlarm(Calendar targetCal){
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
getActivity
with BroadcastService
, how I can do it?I'm just starting studying programming, so I will be very grateful if you could give me some link, or a broad answer.
Upvotes: 1
Views: 3657
Reputation: 424
First of all give permissions in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
Then create the below class to wakeup your phone;
public class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx){
if(wakeLock!=null)wakeLock.release();
PowerManager pm=(PowerManager)ctx.getSystemService(Context.POWER_SERVICE);
wakeLock=pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|
PowerManager.ACQUIRE_CAUSES_WAKEUP|
PowerManager.ON_AFTER_RELEASE,"tag");
wakeLock.acquire();
}
public static void release(){
if(wakeLock!=null)
wakeLock.release();
wakeLock=null;
}
}
Add this code line at the begining of your Alarm Reciever class (inside OnRecieve Method);
WakeLocker.acquire(context);
And this code line at the end of the OnRecieve Method
WakeLocker.release();
Additionally I think it's better to call setExact() method for the Alarm Manager instead of set() method. Because set() method fires within the given minute. Not exactly at the begining of the minute. I prefer setExact() method than set() method.
Upvotes: 3
Reputation: 106
Try these :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
And add this permission in manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Upvotes: 2