我使用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");
}
}
是否可以訪問任何數據庫或在服務中創建的通知? – 2011-03-21 13:20:08
是服務也是一種環境,您可以完全按照您在無gui的活動中所做的相同方式執行。 – senola 2011-03-21 13:24:13
如果要實施自己的自定義服務(矯枉過正),請確保您的服務在工作時自行停止是完整的,否則它會繼續運行,並且可能不會像第二次調用它時那樣出現......或者使用一個「IntentService」來爲您處理這些事情。 – Devunwired 2011-03-22 12:55:24