0

我在活动中有以下内容,它基本上在用户登录应用程序时设置了一个 AlarmManager。然后,AlarmManager 会定期调用另一个从手机数据库中删除事务的活动。这一切都很好。

// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(EntryActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message", "deleting transactions");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(EntryActivity.this, 192837,
                                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);

.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("alarm_message");
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
            myIntent.setAction("com.carefreegroup.startatboot.MyService");
            context.startService(myIntent);
        } catch (Exception e) {
            Toast.makeText(context, "There was an error somewhere, but we still"
                            + " received an alarm", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

.

public class SendOutstandingTransactions extends IntentService {

    private static final String TAG = SendOutstandingTransactions.class.getSimpleName();
    NfcScannerApplication nfcscannerapplication;
    Cursor c;

    @Override
    public void onCreate() {
        nfcscannerapplication = (NfcScannerApplication)getApplication();
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        nfcscannerapplication.loginValidate.deleteTableTransactions();
    }

    public SendOutstandingTransactions() {
        super("SendOutstandingTransactions");
    }
}// end of class

只有在用户首次登录应用程序时才会调用定期删除事务的服务。从那时起它无限期地运行。如果用户重启手机怎么办?该服务只会在用户下次登录时恢复。

我有一个 BootReceiver,但我不知道如何附加 AlarmManager。我尝试了以下方法,但 ALARM_SERVICE 无法解决。这是在正确的路线上吗?

public class MyBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //  Intent myIntent = new Intent(context, SendOutstandingTransactions.class);
        //  myIntent.setAction("com.carefreegroup.startatboot.MyService");
        //  context.startService(myIntent);

        // get a Calendar object with current time
        Calendar cal = Calendar.getInstance();
        // add 5 minutes to the calendar object
        cal.add(Calendar.MINUTE, 1);
        Intent intent = new Intent(MyBootReceiver.this, AlarmReceiver.class);
        intent.putExtra("alarm_message", "deleting transactions");
        // In reality, you would want to have a static variable for the request code instead of 192837
        PendingIntent sender = PendingIntent.getBroadcast(context, 192837,
                                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        //am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15000, sender);
    }
}
4

1 回答 1

1

getSystemService可在Context. (您在 中收到它onReceive

你应该做:

AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
于 2012-10-10T13:14:00.487 回答