0

我使用AlarmManager創建了一個鬧鐘。Android通知問題

Intent intent = new Intent(MyApp.this,NotificationMessage.class); 
PendingIntent sender = PendingIntent.getBroadcast(MyApp.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime,sender); 

這裏是NotificationMessage類。

public class NotificationMessage extends BroadcastReceiver { 
    // Display an alert that we've received a message. 
    // @Override 
    public void onReceive(Context context, Intent intent) { 


     Intent myIntent = new Intent(); 
     myIntent.setClass(context, ScheduleAlert.class); 
     myIntent.setAction(ScheduleAlert.class.getName()); 
     myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
     context.startActivity(myIntent); 
    } 
} 

它調用一個意圖來創建一個通知。爲了獲取通知文本,我必須訪問數據庫。我想爲通知創建聲音和振動。並在頂部欄顯示通知圖標,但沒有視圖。但它在通知時顯示黑屏。如何解決它?

public class ScheduleAlert extends Activity { 

    private String notificationAlart; 

    // ************* Notification and AlarmManager ************// 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


       // get contentText from the database 


     NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       final Notification notifyDetails = new Notification(
         R.drawable.icon, "MyApp", nextAlarmTime); 

       Context context = getApplicationContext(); 
       CharSequence contentTitle = "MyApp"; 
       CharSequence contentText = notificationAlart; 

       Intent notifyIntent = new Intent(context, MyApp.class); 

       PendingIntent pendingIntent = PendingIntent.getActivity(
         ScheduleAlert.this, 0, notifyIntent, 
         android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       notifyDetails.setLatestEventInfo(context, contentTitle, 
         contentText, pendingIntent); 


       notifyDetails.flags = Notification.FLAG_AUTO_CANCEL; 
       notifyDetails.defaults |= Notification.DEFAULT_SOUND 
         | Notification.DEFAULT_VIBRATE; 
       mNotificationManager.notify((int) editEventid, notifyDetails); 
       Log.d(null,"notification set"); 
    } 


} 

回答

1

您的ScheduleAlert是否真的必須是一項活動?不是Service不是更好嗎? Service類不提供任何GUI,因此沒有黑屏。

+0

是否可以訪問任何數據庫或在服務中創建的通知? – 2011-03-21 13:20:08

+0

是服務也是一種環境,您可以完全按照您在無gui的活動中所做的相同方式執行。 – senola 2011-03-21 13:24:13

+0

如果要實施自己的自定義服務(矯枉過正),請確保您的服務在工作時自行停止是完整的,否則它會繼續運行,並且可能不會像第二次調用它時那樣出現......或者使用一個「IntentService」來爲您處理這些事情。 – Devunwired 2011-03-22 12:55:24

1

你的代碼顯示一個空白屏幕,因爲你啓動了一個活動作爲警報觸發的結果,而一個沒有contentView的活動仍然會全屏顯示,但它將是空白的。您應該直接在BroadcastReceiver中構建並激發您的Notification,而不是產生其他系統組件。

public class NotificationMessage extends BroadcastReceiver { 
    // Display an alert that we've received a message. 

    public void onReceive(Context context, Intent intent) { 
     NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); 
     Notification notifyDetails = new Notification(R.drawable.icon, "MyApp", nextAlarmTime); 

     CharSequence contentTitle = "MyApp"; 
     CharSequence contentText = notificationAlart; 

     Intent notifyIntent = new Intent(context, MyApp.class); 

     PendingIntent pendingIntent = PendingIntent.getActivity(
       ScheduleAlert.this, 0, notifyIntent, 
       android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     notifyDetails.setLatestEventInfo(context, contentTitle, 
       contentText, pendingIntent); 


     notifyDetails.flags = Notification.FLAG_AUTO_CANCEL; 
     notifyDetails.defaults |= Notification.DEFAULT_SOUND 
       | Notification.DEFAULT_VIBRATE; 
     mNotificationManager.notify((int) editEventid, notifyDetails); 
     Log.d(null,"notification set"); 
    } 
} 

注意,爲您提供了一個上下文時調用此方法,所以你不必產卵的活動或服務只是爲了獲得一個訪問的目的。

唯一的例外是,如果Notification的構建預計需要很長時間(在訪問數據庫時),在這種情況下,您應該從onReceive()產生一個IntentService來完成這項工作。

希望有助於!

1

您無法從onReceive方法開始您的活動。你必須再次使用通知管理器,並創建另一個PendingIntent來描述當用戶點擊通知時會發生什麼。

考慮使用buzzbox api讓您的生活更輕鬆。 您必須創建一個任務並將您的DB代碼放入doWork方法中。 然後你可以使用一個cron字符串安排你的任務..

更多信息:​​