我有一个活动,用户选择日期和时间,并将日期输入sqlite数据库。从活动开始,我正在启动一个类似于下面的IntentService
Intent intents = new Intent (this,AlarmIntentService.class);
startService(intents);
在intentservice中,我将sqlite数据库中的数据与当前系统时间进行比较,并广播警报。
当我第一次调用intentservice时,只有一个条目进入数据库,它工作正常。但是,当用户将第二个数据插入sqlite数据库并启动Intentservice时,它不起作用。
有没有用新数据更新传递的intentservice?
意图服务onhandleintent如下所述
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final SqliteController db = new SqliteController(getApplicationContext());
List<Greetings> greetings = db.getAllGreetings();
int k = db.getGreetingsCount();
String p = String.valueOf(k);
Log.d("p", p);
if (db.getGreetingsCount() >= 0) {
do {
for (Greetings g : greetings) {
Calendar cal = Calendar.getInstance();
d1 = cal.get(Calendar.DAY_OF_MONTH);
d2 = cal.get(Calendar.MONTH);
d3 = cal.get(Calendar.YEAR);
t1 = cal.get(Calendar.HOUR);
t2 = cal.get(Calendar.MINUTE);
t3 = cal.get(Calendar.AM_PM);
if (t3 == 1) {
t4 = "PM";
} else {
t4 = "AM";
}
tc = String.valueOf(t1) + " : " + t2 + " " + t4;
d = String.valueOf(d3) + "-" + String.valueOf(d2 + 1) + "-" + String.valueOf(d1);
cdt = d + "\t" + tc;
time = g.getTime();
date = g.getDate();
phone = g.getPhoneNumber();
msg = g.getMessage();
id = g.getID();
dnt = date + "\t" + time;
if (dnt.equals(cdt)) {
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
aint.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
showNotification();
db.deleteGreetings(g);
}
}
k = db.getGreetingsCount();
} while (k != 0);
答案 0 :(得分:0)
您可以使用意图过滤器为您的服务电话使用以下代码。
答案 1 :(得分:0)
尝试将所有声明移出循环。它对我有用。
答案 2 :(得分:0)
1º创建文件类:
public class ClassAlarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("BroadCast Alarm", "do anything");
}
}
2º在 ANDROIDMANIFEST 文件中注册广播:
<receiver android:name=".ClassAlarm" >
<intent-filter>
<action android:name="broadCastAlarm" />
</intent-filter>
</receiver>
3º您的启动和停止功能警报
您的 AlarmManager 和 PendingIntent 必须是全局变量。
private static AlarmManager alarm = null;
private static PendingIntent pintent = null;
public static void startAlarm(Context context)
{
if (pintent == null && alarm == null)
{
Intent startIntent = new Intent("broadCastAlarm");
pintent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
alarm = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 0);
//wait 10000ms
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
10000, pintent);
}
}
public static void stop_alarm(Context context)
{
if (alarm != null && pintent != null)
{
pintent.cancel();
alarm.cancel(pintent);
alarm = null;
pintent = null;
}
}