Autore Topic: AlarmManager e notifiche  (Letto 509 volte)

Offline Blodhgard

  • Utente junior
  • **
  • Post: 53
  • Respect: +3
    • Mostra profilo
    • Budget Veloce
  • Dispositivo Android:
    Nexus 5, S4 mini, Galaxy Tab S
  • Play Store ID:
    blodhgard
  • Sistema operativo:
    Windows 8.1
AlarmManager e notifiche
« il: 19 Febbraio 2015, 00:52:20 CET »
0
Salve,
sto cercando di creare una notifica ad un orario prestabilito ma ho incontrato molti problemi.
A volte la notifica non parte e inoltre a volte non cancella le notifiche automatiche vecchie quando cambio ora.

BroadcastReceiver:

Codice (Java): [Seleziona]
public class Notification extends BroadcastReceiver
{
    @Override
    public void onReceive(Context ctx, Intent intent)
    {
        int notificationId = intent.getIntExtra("id", 0);
        Intent resultIntent = new Intent(ctx, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       
        //resultIntent.putExtra("id", notificationId);
        PendingIntent resultPendingIntent =  PendingIntent.getActivity(ctx, 1, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notificationBuilder =  new NotificationCompat.Builder(ctx);
        notificationBuilder.setContentTitle(ctx.getString(R.string.reminder));
        notificationBuilder.setContentText(ctx.getString(R.string.reminder_text_notification));
        notificationBuilder.setTicker(ctx.getString(R.string.app_name));
        notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        notificationBuilder.setVibrate(new long[] { 500, 500, 500 });
        Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher);  
        notificationBuilder.setLargeIcon(bitmap);
        notificationBuilder.setSmallIcon(R.drawable.ic_stat_notification);
        notificationBuilder.setContentIntent(resultPendingIntent);
        notificationBuilder.setAutoCancel(true);

        NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, notificationBuilder.build());        
    }
}

Creazione e cancellazione notifica
Codice (Java): [Seleziona]
AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
                        Intent intent = new Intent(ctx, Notification.class);
                        intent.putExtra("id", item_id);
                        intent.setPackage(ctx.getPackageName());
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, item_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                       
                        if(((ToggleButton) view.findViewById(R.id.togglebutton_reminder_enabled)).isChecked() == false)
                        {
                                editor.putBoolean("pref_reminder_enabled", false).commit();
                               
                                alarmManager.cancel(pendingIntent);
                        }
                        else
                        {
                                alarmManager.cancel(pendingIntent);                     // Delete old Alarm if present
                               
                                editor.putBoolean("pref_reminder_enabled", true);
                                editor.putLong("pref_reminder_time", time_millis).commit();
                               
                                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time_millis, AlarmManager.INTERVAL_DAY, pendingIntent);
                        }      


Manifest:
Codice (XML): [Seleziona]
 <receiver
           android:name=".Notification"
           android:exported="false" />

Idee?