2011-09-02 89 views
0

我想在我的應用程序的android通知欄上顯示選定日期和時間的提醒。但是即使沒有應用程序,也應該填充該提醒。每當我的android設備啓動並且一個選擇的時間有/沒有我的應用程序時,它應該在指定的時間內顯示通知,如報警。 請幫我怎麼做到這一點?如何在Android上設置提醒通知?

我發現了一些關於Alarm Manager,Notification Manager的鏈接。給我的想法和一些鏈接,否則樣本片段。

+1

寫的教程。通知提醒:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ – Blundell

回答

1

使用此增加一個報警

Intent intent = new Intent(this, TheServiceYouWantToStart.class); 
PendingIntent pending = PendingIntent.getService(this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
alarm.set(AlarmManager.RTC_WAKEUP, Time_To_wake, pending); 
+0

如果我從我的應用程序出來,上述代碼不起作用。我希望警報服務能夠工作沒有我的appl也。 – ADIT

+0

是的這將工作...它將啓動'TheServiceYouWantToStart' 你必須添加代碼onStartCommand –

1
public static void notifyIcon(Context context){ 

NotificationManager notifier = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notification = new Notification(R.drawable.your_app_notification, "", System.currentTimeMillis()); 

/** 
*Setting these flags will stop clearing your icon 
*from the status bar if the user does clear all 
*notifications. 
*/ 
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 
    RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout); 
    notification.contentView = contentView; 

//If you want to open any activity on the click of the icon. 

    Intent notificationIntent = new Intent(context, YourActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.contentIntent = contentIntent; 

    notification.setLatestEventInfo(context, "title", null, contentIntent); 
    notifier.notify(1, notification); 

//To cancel the icon ... 
    notifier.cancel(1); 

} 

這裏是custom_notification_layout.xml

<?xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="3dp" > 

</LinearLayout> 

注:請不要忘記在適當的時候如果上述標誌設置爲清除您的應用程序圖標。