我已经使用 AlarmManager 创建了一个警报。
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);
}
}
它正在调用 Intent 来创建通知。为了获取通知文本,我必须访问数据库。我想为通知创建声音和振动。并且还在顶部栏显示通知图标,但没有视图。但它在通知时显示黑屏。如何解决?
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");
}
}