2012-07-11 13 views
6

Chcę używać powiadomienia lokalnego w Androidzie dla mojej aplikacji. Jeśli aplikacja nie zostanie otwarta przez 24 godziny, wysyłane jest powiadomienie lokalne. Czy ktoś może mi powiedzieć, jak należy to zrobić.Wysyłanie lokalnych powiadomień w Androidzie

+0

dlaczego nie u używać alarm – Zamani

+0

myślę, że należy stworzyć usługę, a następnie sprawdzić czas, ale za pokazanie zgłoszenie u musi czytać o nim. =) – Gorets

+0

Gorets, masz rację, muszę użyć jakiejś usługi, ponieważ powiadomienie zostanie uruchomione po zamknięciu aplikacji. Czy możesz podać mi samouczek dla tego –

Odpowiedz

4

Zobacz: Local Notifications in Android? Powinieneś być w stanie zaplanować Intent z menadżerem alarmów co godzinę.

+1

Dziękuję za szybką odpowiedź, ale można użyć menedżera alarmu, jeśli aplikacja jest zamknięta. W jaki sposób powiadomienie zostanie uruchomione, jeśli aplikacja zostanie zamknięta? –

+0

Tak, menedżer alarmów nadal może być używany, nawet gdy aplikacja jest zamknięta. Jednak nie będzie można ustawić menedżera alarmów, gdy aplikacja jest zainstalowana, tylko wtedy, gdy aplikacja jest ładowana (co najmniej raz) (patrz: http://stackoverflow.com/a/8492846/986105). Spójrz na to, aby utworzyć powiadomienie za pomocą menedżera alarmów: http://smartandroidians.blogspot.com/2010/04/alarmmanager-and-notification-in.html – KrispyDonuts

-1

Jeśli chcesz odpalić lokalnym powiadomienia z dużym IE danych z tekstu wielowierszowego w jednym powiadomieniu z tytułem, Ticker, ikona, dźwięk .. Zastosowanie Poniższy kod .. Myślę, że to pomoże ..

 Intent notificationIntent = new Intent(context, 
       ReminderListActivity.class); 



     notificationIntent.putExtra("clicked", "Notification Clicked"); 
     notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
       | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one activity 


      // Invoking the default notification service 

      NotificationManager mNotificationManager; 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        context); 
      Uri uri = RingtoneManager 
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      mBuilder.setContentTitle("Reminder"); 
      mBuilder.setContentText("You have new Reminders."); 
      mBuilder.setTicker("New Reminder Alert!"); 
      mBuilder.setSmallIcon(R.drawable.clock); 
      mBuilder.setSound(uri); 
      mBuilder.setAutoCancel(true); 

      // Add Big View Specific Configuration 
      NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 
      String[] events = null; 

       events[0] = new String("Your first line text "); 
       events[1] = new String(" Your second line text"); 



      // Sets a title for the Inbox style big view 
      inboxStyle.setBigContentTitle("You have Reminders:"); 

      // Moves events into the big view 
      for (int i = 0; i < events.length; i++) { 
       inboxStyle.addLine(events[i]); 
      } 

      mBuilder.setStyle(inboxStyle); 

      // Creates an explicit intent for an Activity in your app 
      Intent resultIntent = new Intent(context, 
        ReminderListActivity.class); 

      TaskStackBuilder stackBuilder = TaskStackBuilder 
        .create(context); 
      stackBuilder.addParentStack(ReminderListActivity.class); 


      // Adds the Intent that starts the Activity to the top of the stack 


      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder 
        .getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); 

      mBuilder.setContentIntent(resultPendingIntent); 
      mNotificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 


      // notificationID allows you to update the notification later on. 


      mNotificationManager.notify(999, mBuilder.build()); 
1
Intent intent = new Intent(context, yourActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder b = new NotificationCompat.Builder(context); 

    b.setAutoCancel(true) 
    .setDefaults(Notification.DEFAULT_ALL) 
    .setWhen(System.currentTimeMillis())   
    .setSmallIcon(R.drawable.ic_launcher) 
    .setTicker("notification")    
    .setContentTitle("notification") 
    .setContentText("notification") 
    .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND) 
    .setContentIntent(pIntent) 
    .setContentInfo("Info"); 


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, b.build()); 
Powiązane problemy