在我的應用我要去使用此代碼使用的通知:Android:是否可以設置通知在需要的時間通知?即使項目沒有運行?
private void addDefaultNotification(){
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence text = "Notification Text";
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Sample notification text.";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, NotificationViewer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon,text,when);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;
notification.defaults |= Notification.DEFAULT_LIGHTS;
//notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification);
}
了RightNow我收到notofication。通過調用該函數。但我想要的是,它應該通知用戶,即使應用程序沒有在設備上運行,並且通知應該在期望的時間通知。
可能嗎? 如果是,那麼請幫助我。 謝謝。
編輯:
public class AlarmNotificationReceiver extends BroadcastReceiver{
//private Intent intent;
private NotificationManager notificationManager;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long value1 = intent.getLongExtra("param1", 0);
String value2 = intent.getStringExtra("param2");
addTwoMonthNotification();
}
}
我也一樣,但不能在接收器類來創建通知進行。爲什麼?我該怎麼做?
好的感謝您的回覆。讓我試試看。 – 2011-12-23 12:14:48
第三個是效用函數嗎?如果是那麼我必須打電話給它?假設如果我使用toogleButton來開啓/關閉alerm,那麼我需要在切換「開啓」時調用該函數嗎? – 2011-12-23 12:22:54
是的,您應該在您想要安排鬧鐘的地方打電話。 – 2011-12-23 12:29:44