0

在我的應用我要去使用此代碼使用的通知: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(); 

    } 
} 

我也一樣,但不能在接收器類來創建通知進行。爲什麼?我該怎麼做?

回答

2

是的,它是可能的。

您需要在AlarmManager中註冊您的意圖,並讓通知接收器類在需要運行時等待來自AlarmManager的通知。

基本上你將需要:

  1. 通知Intent類(意向的子類)

    public class MyNotificationIntent extends Intent { 
        // basic implementation ... 
    } 
    
  2. NotificationReceiver類,廣播接收器的子類。你從哪兒AlarmManger收到通知,需要運行你的代碼,以顯示通知(您已經擁有了的東西)

    public class AlarmNotificationReceiver extends BroadcastReceiver{ 
    
        @Override 
        public void onReceive(Context context, Intent intent){ 
    
         long value1 = intent.getLongExtra("param1", 0); 
         String value2 = intent.getStringExtra("param2"); 
    
         // code to show the notification 
         ... 
         notificationManager.notify(...); 
        } 
    } 
    
  3. 效用函數註冊您的通知中AlarmManager

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new MyNotificationIntent("com.alex.intent.action.ALARM_NOTIFICATION", 
        Uri.parse("notification_alarm_id"), context, AlarmNotificationReceiver.class); 
    intent.putExtra("param1", value1); 
    intent.putExtra("param2", value2); 
    
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeToRun, pendingIntent); 
    
+0

好的感謝您的回覆。讓我試試看。 – 2011-12-23 12:14:48

+0

第三個是效用函數嗎?如果是那麼我必須打電話給它?假設如果我使用toogleButton來開啓/關閉alerm,那麼我需要在切換「開啓」時調用該函數嗎? – 2011-12-23 12:22:54

+0

是的,您應該在您想要安排鬧鐘的地方打電話。 – 2011-12-23 12:29:44

1

http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

AlarmManager類提供訪問系統報警服務。這些允許您安排您的應用程序在未來的某個時間點運行。當警報響起時,已經註冊的Intent被系統廣播,如果目標應用程序尚未運行,則會自動啓動。

你也可以閱讀這一個:http://android.arnodenhond.com/tutorials/alarm-notification

你應該能夠在所需時間通知你的用戶沒有任何問題

ADDED

或者,你可以這樣做:

new Handler().postDelayed(new Runnable() { public void run() { 
      //Shoot your notification here 
     } 
    }, 1000 * 60 * 5); 

** OR **

http://developer.android.com/resources/articles/timed-ui-updates.html

private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     final long start = mStartTime; 
     long millis = //something; 
     int seconds = //something; 
     int minutes = //something; 
     seconds  =//something; 

     mHandler.postAtTime(this, 
       start + (((minutes * 60) + seconds + 1) * 1000)); 
    } 
}; 
+0

請你有助於將其用於我的代碼? – 2011-12-23 11:53:11

+0

直接編碼.....哇:),首先你應該理解@ Profete162在這裏提供的概念,並至少嘗試一次。 – 2011-12-23 13:41:54

+0

我添加了一個工作示例的代碼 – 2011-12-23 16:58:21

相關問題