我正在尝试构建一个简单的应用程序,它只会在特定的日期/时间向设备发送通知。我已经能够让通知部分工作,但似乎无法得到这一天&时间问题解决了。有什么想法吗?
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class NotificationsActivity extends Activity {
int notificationID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
displayNotification();
}
protected void displayNotification()
{
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.ic_launcher,
"Reminder: Meeting starts in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with customer at 3pm...";
notif.setLatestEventInfo(this, from, message, pendingIntent);
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notificationID, notif);
}
}
答案 0 :(得分:0)
您需要使用AlarmManager
来安排这些事件。然后,从回调中,您将显示通知。
参见例如Creating a Notification at a particular time through Alarm Manager
小心,因为当设备重新启动时,AlarManager将被清除(如果需要,您可能需要重新注册然后在启动时完成)。
答案 1 :(得分:0)