2013-10-10 170 views
0

這裏有兩個問題。我嘗試讓用戶選擇特定時間以使應用提醒用戶使用應用。一旦用戶設置時間,例如晚上8點。通知應該每天彈出。但是,我用過之後android狀態欄沒有通知彈出

"NotificationManager" and "Notification" 

首先,沒有什麼彈出。例如,現在是AU的1:19上午,然後,我將這個應用程序設置爲凌晨1:20,以在狀態欄中顯示通知作爲測試。

其次,如果在我的的TabBar類別I

nm.cancel(getIntent().getExtras().getInt("NotifID")); 

它會得到一個空指針異常

這裏是我的代碼:

TabBar.class

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
nm.cancel(getIntent().getExtras().getInt("NotifID")); 

Notification.class

package com.example.tabpro; 

import java.util.Calendar; 
import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TimePicker; 

public class SettingNotification extends Activity{ 

TimePicker timePicker; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.setting_notification); 

    Button btnset = (Button) findViewById(R.id.btnSetAlarm); 
    btnset.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      timePicker = (TimePicker) findViewById(R.id.timePicker_settime); 

      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); 
      calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); 
      calendar.set(Calendar.SECOND, 0); 

      Intent i = new Intent("com.example.tabpro.DisplayNotification"); 
      Intent i2 = new Intent(SettingNotification.this, TabBar.class); 
      i.putExtra("NotifID", 1);         

      PendingIntent displayIntent = PendingIntent.getActivity(
       getBaseContext(), 0, i, 0); 

      alarmManager.set(AlarmManager.RTC_WAKEUP, 
        calendar.getTimeInMillis(), displayIntent); 
      startActivity(i2); 

     } 


    }); 



} 


} 

DisplayNotification.class

package com.example.tabpro; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 

public class DisplayNotification extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    int notifID = getIntent().getExtras().getInt("NotifID"); 

    Intent i = new Intent("com.example.tabpro.TabBar"); 

    i.putExtra("NotifID", notifID); 

    PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0); 

    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
    Notification notif = new Notification(R.drawable.ic_launcher, "Time's up!", System.currentTimeMillis()); 

    CharSequence from = "AlarmManager - Time's up!"; 
    CharSequence message = "This is your alert, courtesy of the AlarmManager";   
    notif.setLatestEventInfo(this, from, message, detailsIntent);  

    finish(); 
} 

} 
+0

'設置()'可以給你的時間在過去,如果小時/分/秒是在今天的過去。你需要檢查這一點,如果需要添加一天。 – CommonsWare

回答

0

你需要調用notify()NotificationManager實際顯示通知。由於您從未調用此方法,因此您的通知從不顯示。

您的NullPointerException可能是一個不同的問題。爲此,你應該自己做一些調試。首先,你需要確定什麼是空的。然後確定它爲什麼是空的並修復它。在您的代碼中,導致TabBar中的NPE的最可能的候選者是nm,getIntent()getExtras()

0

希望這會有所幫助: 您需要調用notify()方法在設備的通知托盤中顯示通知。

下面是該代碼:使用

Notification notification; 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,  YourActivity.class), 0); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
notification = builder.setContentIntent(contentIntent) 
       .setSmallIcon(icon).setTicker(yourappname).setWhen(0) 
       .setAutoCancel(true).setContentTitle(yourappname) 
       .setContentText(yourmessage).build(); 
notificationManager.notify(0, notification);