2013-12-06 2 views
0

что я хочу сделать, есть уведомление, чтобы сообщить пользователю о предстоящей задаче, которая была добавлена ​​в список дел EVEN без открытия приложения todo. Кстати, я использовал BroadcastReceiver, потому что я прочитал android, чтобы проверить, есть ли какое-либо сообщение, полученное ими. Я знаю, что чего-то не хватает, потому что у меня есть все в mainactivity.java, поэтому это будет сделано, только открыв приложение.добавить уведомление в список дел в android?

вот мой BroadcastReceiver файл общественного класса Broadcast расширяет BroadcastReceiver {

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

    try { 
     Bundle bundle = intent.getExtras(); 
     String message = bundle.getString("alarm_message"); 
     Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(
       context, 
       "There was an error somewhere, but we still received an alarm", 
       Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 

    } 

    Toast.makeText(context, "received an alarm", Toast.LENGTH_SHORT).show(); 

} 

}

и вот мой mainactivity.java

общественный класс MainActivity расширяет активность {

public NotificationCompat.Builder builder; 
public NotificationManager notificationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // NOTIFICATION 

    AlarmManager alarmMang = (AlarmManager) getSystemService(ALARM_SERVICE); 

    // ---get current date and time--- 

    Calendar calender = Calendar.getInstance(); 
    calender.add(Calendar.MINUTE, 5); 


    Intent noteIntent = new Intent(this, Broadcast.class); 
    PendingIntent pendI = PendingIntent.getActivity(this, 0, noteIntent, 0); 
    long fireTime = System.currentTimeMillis(); 

    builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Notifications Example") 
      .setContentText("This is a test notification"); 

    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    // // BUTTON NOT 
    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      notificationManager.notify(0, builder.build()); 
     } 
    }); 

    // // AET ALARM 



    Toast.makeText(this, "lilili", Toast.LENGTH_SHORT).show(); 
    alarmMang.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 
      pendI); 


    notificationManager.notify(2332323, builder.build()); 
    // finish(); 

} 
+1

Что вы пытаетесь? Что вы прочитали? Что конкретно не касалось документации? Чем конкретнее ваш вопрос, тем более вероятно, что вы получите помощь здесь. – EJK

+0

http://www.vogella.com/articles/AndroidNotifications/article.html –

ответ

0

Примечание: Показать основное уведомление является относительно простым, этот пример кода может помочь вам понять, как это работает

// Notification.Builder is used to create the notification 
Notification.Builder builder = new Notification.Builder(this); 
// This set the title of the notification 
builder.setContentTitle("Title") 
// This set the content of the notification (the text inside the notification) 
.setContentText("Content of the notification") 
// Here you set the icon of the notification 
.setSmallIcon(R.drawable.ic_action_copy); 

// Build the notification and save it 
Notification notification = builder.build(); 
// To show the notification you need to use the NotificationManager, to get it you should use getSystemService 
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
// .. then you can use .notify to show the notification 
// 0 is the ID of the notification, you can use this ID to update the notification later. 
// notification is the notification to show (you can use builder.build() inside too.) 
manager.notify(0, notification); 

Но если вам нужно больше информации об уведомлении вы должны читать this, это очень хороший учебник и объяснять все с изображениями тоже.

Смежные вопросы