我想制作仅在每月的第1天,第2天和第3天出现的本地通知,例如上午11点。我在12:30,14:30和18:30连续3天设置了通知,但它只在14:30显示一次。 当我检查android显示器那个小时它看起来像android只是跳过那一秒?! 这是我的代码:
这是我在onCreate
主要活动中设置的服务`@EService
public class NotifiService extends Service {
public int counter = 0;
public NotifiService(Context applicationContext) {
super();
Log.w("here!", "here i am");
}
public NotifiService(){
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
Log.i("-----b", "onStartCommand");
startTimer();
return Service.START_STICKY; //super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro");
sendBroadcast(broadCastIntent);
stopTimerTask();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Background
public void checkDate(){
Log.w("f", "if working");
Calendar cal = Calendar.getInstance();
int d = cal.get(Calendar.DAY_OF_MONTH);
int h = cal.get(Calendar.HOUR_OF_DAY);
int s = cal.get(Calendar.SECOND);
int m = cal.get(Calendar.MINUTE);
if( d==17 || d == 18 || d == 19){
if (h == 12 || h == 14 || h == 16) {
if (m == 30) {
if (s == 30) {
Log.w("notify", "working>>>>");
Intent intent = new Intent(getApplicationContext(), MainActivity_.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
b.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_app_icon)
.setTicker("FabReminder")
.setContentTitle("FabReminder")
.setContentText(getString(R.string.notification))
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
//.setContentInfo("Tap this bar and select shop");
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
}
}
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void stopTimerTask(){
if (timer != null){
timer.cancel();
timer = null;
}
}
public void initializeTimerTask(){
timerTask = new TimerTask() {
@Override
public void run() {
checkDate();
}
};
}
public void startTimer(){
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask,1000,1000);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.w("killed task >>>"," task killed");
Intent broadCastIntent = new Intent("com.Yyyyy.Xxxxx.Yyyyy.BootBro");
sendBroadcast(broadCastIntent);
}`
这是我的BroadcastReceiver:
`public class BootBro扩展了BroadcastReceiver { public int MID;
@Override
public void onReceive(Context context, Intent intent) {
//context.startService(new Intent(context, NotifiService_.class));
Log.w(BootBro.class.getSimpleName(), "Service stops!");
context.startService(new Intent(context, NotifiService_.class));
}
}`
和AndroidManifest
`<service android:name="com.Yyyyy.Xxxx.Yyyyy.NotifiService_" android:enabled="true"/>
<receiver android:name="com.Yyyyy.Xxxx.Yyyy.BootBro" android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter><action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<action android:name="com.Yyyyy.Xxxx.Yyyy.BootBro"/>
</intent-filter>
</receiver>`
答案 0 :(得分:1)
使用 AlarmManager 可以轻松设置此方法。
此类提供对系统警报服务的访问。这些允许您安排应用程序在将来的某个时间运行... More
警报具有以下特征:
官方Android文档提供了一个教程,您可以按照here
进行操作此处还有一个simple app示例,您可以设置闹钟唤醒。