我不想在具体时间申请临时通知。 具体时间一米计算法非常复杂,但现在情况并非如此。
每天的通知时间各不相同:
12/01/12 | 13/01/12 | 14/01/12
n1:02:00 | n1:02:05 | n1:02:06
n2:07:00 | n2:07:08 | ...
n3:12:00 | n3:12:02 | ...
n4:14:00 | n4:13:59 | ...
n5:20:00 | n5:20:01 | ...
我创建了两个活动类别,用于在特定时间进行通知:
private void createNotification(final String contentTitle, final String contentText,
final String tickerText) {
calculateTimes();
setFirstMillis(getNextTime());
createStatusBarNotification(contentTitle, contentText, tickerText);
Notification note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, VakActivity.class), 0);
note.setLatestEventInfo(this, contentTitle, contentText, intent);
note.vibrate = new long[]{1000,1000,1000,1000,1000};
note.defaults |= Notification.DEFAULT_LIGHTS;
note.sound = Uri.parse(getAlarmTone());
note.flags |= Notification.FLAG_AUTO_CANCEL;
note.flags |= Notification.FLAG_INSISTENT;
notificationManager.notify(NOTIF_ID, note);
}
private void createStatusBarNotification(final String contentTitle,
final String contentText, final String tickerText) {
Date date = new Date(getFirstMillis());
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
createNotification(contentTitle, contentText, tickerText);
}
};
timer.schedule(timerTask, date);
}
我开始采用以下方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
...
...
createStatusBarNotification("Title", "Text",
"tickerText");
}
但我要开始这项通知。
我试图打造一个新班子。
public class StartNotificationService extends Service
但我不知道我需要在这个类别中写什么,以及这一类别在何时和从哪里运作。
最新情况:
我制定了新的活动方法,我从Create方法中呼吁采用。
public void setOneTimeAlarm() {
Intent intent = new Intent(this, StartNotificationService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
am.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5 * 1000), pendingIntent);
}
1. 启动认证服务班:
public class StartNotificationService extends BroadcastReceiver {
private static final int NOTIF_ID = 1234;
private Pror pror;
private Context cntx;
private NotificationManager notificationManager;
@Override
public void onReceive(Context context, Intent intent) {
cntx = context;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
createStatusBarNotification("Title", "Text",
"tickerText");
}
private void createStatusBarNotification(final String contentTitle,
final String contentText, final String tickerText) {
Date date = new Date(pror.getFirstMillis());
Log.i("date", date.toString());
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
createNotification(contentTitle, contentText, tickerText);
}
};
timer.schedule(timerTask, date);
}
private void createNotification(final String contentTitle, final String contentText,
final String tickerText) {
pror.calculateVak();
pror.setFirstMillis(pror.getNextVak());
createStatusBarNotification(contentTitle, contentText, tickerText);
Notification note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(cntx, 0, new Intent(cntx, VakActivity.class), 0);
note.setLatestEventInfo(cntx, contentTitle, contentText, intent);
note.vibrate = new long[]{1000,1000,1000,1000,1000};
note.defaults |= Notification.DEFAULT_LIGHTS;
note.sound = Uri.parse(pror.getAlarmTone());
note.flags |= Notification.FLAG_AUTO_CANCEL;
note.flags |= Notification.FLAG_INSISTENT;
notificationManager.notify(NOTIF_ID, note);
}
}
www.un.org/Depts/DGACM/index_spanish.htm 我想从活动到启动发放通知服务时发放变量。 我怎样做?
问题很可变。 我的这种变数是活动(现在的偏好,......),我想起起起“启动通知服务”类的例子。