为什么启动完成后重启不起作用?

时间:2013-09-28 18:47:06

标签: android boot alarms

我有一些应用程序使用警报,我做了一些代码来重启设备时从sqlite重置所有警报但重启时没有发生任何事情..什么是黄?

这是我的代码:

的manifest.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);
        }

    }

}

intentService

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);
    }

    // ==============================================================================

}

0 个答案:

没有答案