我有一些使用警报的应用程序,我做了一些代码来在重新启动设备时从 sqlite 重置所有警报,但是重新启动时没有任何事情发生.. 王是什么?
这是我的代码:
清单.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ResetAlarmsService" >
</service>
接收者
package com.engahmedphp.facebookcollector;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootCompletedReceiver extends BroadcastReceiver {
// ==============================================================================
@Override
public void onReceive(Context context, Intent intent) {
General general = new General(context);
if (general.isNetworkAvailable()) {
Log.e("boot", "boot");
Intent resetAlarms = new Intent(context, ResetAlarmsService.class);
context.startService(resetAlarms);
}
}
}
意向服务
package com.engahmedphp.facebookcollector;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
public class ResetAlarmsService extends IntentService {
DAOPages dbPages;
Context context;
String fpageid;
Cursor c;
Long nextAlarm, interval;
// Pages Table Columns names
private static final String PA_FID = "fid";
private static final String PA_POLLING_INTERVAL = "polling_interval";
private static final String PA_NEXT_ALARM = "next_alarm";
// ==============================================================================
public ResetAlarmsService() {
super("ResetAlarmsService");
}
// ==============================================================================
@Override
public void onDestroy() {
dbPages.closeDatabase();
// TODO Auto-generated method stub
super.onDestroy();
}
// ==============================================================================
@Override
protected void onHandleIntent(Intent intent) {
Cursor c = dbPages.getActivePages();
Log.i("handle", "boot");
if (c.getCount() > 0) {
Log.i("if", "boot");
Long curTime = System.currentTimeMillis();
do {
fpageid = c.getString(c.getColumnIndex(PA_FID));
nextAlarm = c.getLong(c.getColumnIndex(PA_NEXT_ALARM));
if (nextAlarm > curTime) {
Log.i("> from", "boot");
// Long nextAlarm = System.currentTimeMillis() + (1 * 30 *
// 1000);
// Long nextAlarm = System.currentTimeMillis() +
// (Long.parseLong(interval) * 60 * 1000);
Intent alarmIntent = new Intent(context,
FeedsReceiver.class);
alarmIntent.putExtra("fid", fpageid);
alarmIntent.setAction(fpageid);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(), 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarm,
pendingIntent);
dbPages.updateNextAlarm(fpageid, nextAlarm);
} else {
Log.i("< from", "boot");
interval = Long.parseLong(c.getString(c
.getColumnIndex(PA_POLLING_INTERVAL))) * 60 * 1000;
while (nextAlarm < curTime) {
nextAlarm = nextAlarm + interval;
}
Intent alarmIntent = new Intent(context,
FeedsReceiver.class);
alarmIntent.putExtra("fid", fpageid);
alarmIntent.setAction(fpageid);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(), 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarm,
pendingIntent);
dbPages.updateNextAlarm(fpageid, nextAlarm);
}
} while (c.moveToNext());
}
}
// ==============================================================================
public int onStartCommand(Intent intent, int flags, int startId) {
context = getApplicationContext();
dbPages = new DAOPages(context);
dbPages.open();
Log.i("reset", "start");
return super.onStartCommand(intent, flags, startId);
}
// ==============================================================================
}