Estoy llamando creando una alarma de esta manera. (y lo hace bien) La alarma se inicia desde un fragment, con un botón.. y noto que allí no esta el problema. Coloco parte del codigo donde lo inicio.
alarmMgr = (AlarmManager)getContext().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getContext().getApplicationContext(), AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(getContext().getApplicationContext(), 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.set(Calendar.HOUR_OF_DAY, 8);
//calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 2, alarmIntent);
Tengo creado el receiver, el cual es llamado correctamente por la alarma. Cada dos minuto lo llama y noto que se ejecuta.
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService.class);
service1.setData((Uri.parse("custom://" + System.currentTimeMillis())));
ContextCompat.startForegroundService(context, service1 );
Log.d("ALARMA", " ALARM RECEIVED!!!");
Incluso veo que se muestra el log. El problema que tengo es que no se ejecuta el servicio llamado NotificationService. Probé de muchas maneras pero no logro ejecutarlo.
public class NotificationService extends IntentService {
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
// GPS
RequestQueue requestQueue;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
double latitude,longitude;
LocationManager locationManager;
Location location;
//Location mLastLocation;
//mLastLocation = new Location(provider);
public NotificationService(String name) {
super(name);
}
public NotificationService() {
super("SERVICE");
}
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onHandleIntent(Intent intent2) {
Log.d("ALARMA", " Dentro de NotificationService");
String NOTIFICATION_CHANNEL_ID = getApplicationContext().getString(R.string.app_name);
Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
Resources res = this.getResources();
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Toast.makeText(getApplicationContext(), "A) Latgps",Toast.LENGTH_SHORT).show();
Log.i( "msg", "Servicio reiniciado");
String message = getString(R.string.new_notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final int NOTIFY_ID = 0; // ID of notification
String id = NOTIFICATION_CHANNEL_ID; // default_channel_id
String title = NOTIFICATION_CHANNEL_ID; // Default Channel
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notifManager == null) {
notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(context, id);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notification) // required
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_notification))
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
startForeground(1, notification);
} else {
pendingIntent = PendingIntent.getActivity(context, 1, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_notification))
.setSound(soundUri)
.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
.setContentText(message).build();
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
Que me puede estar pasando?