我有一個由alarmmanager每5秒調用一個intentservice類。 Alarmmanager調用intentservice,它工作正常。但是當它調用時,它會創建新的intentservice。我只想調用intentService的onHandleIntent方法不想創建新的方法。這裏是我的代碼:如何重複調用intentservice onHandleIntent方法而不創建新的intentservice
IntentService類:
public class MyIntentService extends IntentService {
private static final String serviceName = "MyIntentService";
public MyIntentService() {
super(serviceName);
}
public void onCreate() {
super.onCreate();
Log.d("Servis", "onCreate()"); //this is called every 5 seconds too
}
@Override
protected void onHandleIntent(Intent intent) {
//do something
}
}
設置alarmManager爲IntentService
public void setAlarm(View v)
{
Calendar cal = Calendar.getInstance();
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
long interval = 1000 * 5;
Intent serviceIntent = new Intent(context, MyIntentService.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(context, 12345, serviceIntent,PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),interval, servicePendingIntent
);
}
非常感謝你的啓發:) – gencer