我需要做一个提示,它会在几个特定时间通知每一天 这里只是举个例子我叫它两次但是 两次调用setInexactRepeating对我不起作用 我得到的通知只是因为第一次调用setInexactRepeating 请告诉我该怎么办
这是我的代码..
package aadil.reminder.health;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
NotificationManager nm;
AlarmManager am;
Calendar clndr=Calendar.getInstance();
static final int unique=18493;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am=(AlarmManager) getSystemService(ALARM_SERVICE);
//Toast.makeText(getApplicationContext(), "button clicked", Toast.LENGTH_LONG).show();
Intent intent = new Intent("intent.AADIL_INTENT");
intent.putExtra("msg", "blah blah");
intent.putExtra("msg", "class 2");
//intent.putExtra("msg1", "blah msg 2");
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
PendingIntent p2 = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+2000,3000, pi);
//intent.putExtra("msg", "class 1");
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3000,2000, p2);
}
}
打包aadil.reminder.health;
public class AlarmReciever extends BroadcastReceiver {
int i = 0;
@Override
public void onReceive(Context arg0, Intent arg1) {
NotificationManager nm = (NotificationManager) arg0.getSystemService(arg0.NOTIFICATION_SERVICE);
Intent intent=new Intent().addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi= PendingIntent.getBroadcast(arg0, 0, intent, 0);
String body= arg1.getExtras().getString("msg");
String title="wake up";
Notification n=new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
n.setLatestEventInfo(arg0, title, body, pi);
n.flags = n.flags | n.FLAG_AUTO_CANCEL;
n.defaults=Notification.DEFAULT_ALL;
nm.notify(i,n);
i++;
}
}
答案 0 :(得分:0)
您的两个待处理意图是相同的,因此后一个调用将替换预定警报列表中的前一个待处理意图。尝试从第二个Intent创建p2,而不是使用相同的意图来创建PendingIntent。
此外,您请求的间隔不是AlarmManager包中定义的常量之一,例如AlarmManager.INTERVAL_FIFTEEN_MINUTES
。如果请求的间隔与这些常量之一不匹配,则Android将使用setRepeating()
(至少在SDK< 19上)安排警报,从而失去setInexactRepeating()的节能优势。
您可能还会发现我的其他答案here很有用。