Reputation: 1973
I'm trying to create a reminder app for tablets. My problem is that if the tablet is in sleep mode the alarm isn't getting called. i tried a lot off project on github none of them wase working when my tablet wase in sleep mode.
My code is the following:
The code to set the alarm:
Intent intent = new Intent(getApplicationContext(),RingAlarmReceiver.class);
Intent intent = new Intent("kidsplaylist.info.waketest.MyWakefulReceiver");
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(),0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent);
The code of the receiver:
public class MyWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WakeLocker.acquire(context);
// Start the service, keeping the device awake while the service is
// launching. This is the Intent to deliver to the service.
Intent service = new Intent(context, MyIntentService.class);
startWakefulService(context, service);
}
}
The code for the service that is supposed to ring the alarm:
public class MyIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
// Do the work that requires your app to keep the CPU running.
String song = Settings.System.DEFAULT_RINGTONE_URI.toString();
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(song));
mediaPlayer.prepare();
mediaPlayer.setLooping(false);
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
MyWakefulReceiver.completeWakefulIntent(intent);
}
}
Can anyone with experience in such thick advice me how to fix it
Thank's a lot
Avi
P.B: When the device is connected to the charger or when the screen is on it works OK The problem is when the device screen is off.
Upvotes: 0
Views: 1127
Reputation: 1006539
Note that on Android 6.0+, Doze mode and app standby will affect AlarmManager
events.
Beyond that, using an IntentService
to directly play media will not work well. Once onHandleIntent()
returns, the IntentService
is destroyed, and your process will typically be terminated shortly thereafter.
I strongly recommend that you raise a Notification
that uses this media as a ringtone. That would eliminate the need for the service entirely (you could raise the Notification
in onReceive()
, since that should be fairly quick to execute). It gives the user more control over whether the music plays (via Notification
controls on Android 5.0+), and it gives the user a straightforward way to shut it up (swipe away the Notification
).
If you insist upon playing the media yourself, you will need to use a regular Service
and manage your own WakeLock
, so that you can keep all that outstanding while the media is playing. Use an OnCompletionListener
to stopSelf()
the service and release()
the WakeLock
when the media is done.
Upvotes: 1