开发者

Android Recall Service

开发者 https://www.devze.com 2023-02-05 23:25 出处:网络
If i am in an activity i call a service b using the following code: String start = \"start\"; Intent i = new Intent(QScheduleActivity.this, UploadService.class);

If i am in an activity i call a service b using the following code:

String start = "start";
    Intent i = new Intent(QScheduleActivity.this, UploadService.class);
    i.putExtra("start", start);
    startService(i)

Once i am inside this service, how do i recall the service from within the service if i need to?

Inside the else i am trying to recall the service like this:

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 30);
        String start = "start";
    开发者_开发知识库    Intent i = new Intent(UploadService.this, UploadService.class);
        i.putExtra("start", start);
        PendingIntent sender = PendingIntent.getBroadcast(this, 192837, i, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 


Inside the else i am trying to recall the service like this:

You are asking for a getBroadcast() PendingIntent. If your goal is to have the alarm call startService(), you should be using a getService() PendingIntent.


Have a look at the AlarmManager class. I think it provides the functionality you need, though perhaps not in quite the way you're describing it.

From the documentation:

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

0

精彩评论

暂无评论...
验证码 换一张
取 消