1
我想取消舊的意圖,並與最新的意圖如何停止Android系統從重新傳遞舊意向IntentService?
例如進行:
在我的MainActivity: -
for(int i=0 ; i<10; i++){
Intent intent = new Intent(mContext,MService.class);
intent.putExtra("text", i);
mContext.startService(intent);
}
在我IntentService: -
public class MService extends IntentService{
private static final String TAG = "MService";
public MService() {
super(TAG);
setIntentRedelivery(true);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG,"onHandleIntent");
Bundle bundle = intent.getExtras();
String value = (String)(bundle.get("text"));
Log.d(TAG, "Received value---"+value);
SystemClock.sleep(2000);
}
}
而且我的日誌看起來像:
MService: onCreate
MService: onStartCommand
MService: onHandleIntent
MService: onStartCommand
MService: onStartCommand
MService: onStartCommand
MService: Received value---0
MService: onStartCommand
MService: onStartCommand
MService: onStartCommand
MService: onStartCommand
MService: onStartCommand
MService: onStartCommand
MService: Received new location
MService: onHandleIntent
MService: Received value---1
MService: onHandleIntent
MService: Received value---2
MService: onHandleIntent
MService: Received value---3
MService: onHandleIntent
MService: Received value---4
MService: onHandleIntent
MService: Received value---5
MService: onHandleIntent
MService: Received value---6
MService: onHandleIntent
MService: Received value---7
MService: onHandleIntent
MService: Received value---8
MService: onHandleIntent
MService: Received value---9
我要處理,而不是處理2至8
請介紹更多的細節您的問題的能力,創建自己的
Service
子你自己的版本。你的確切工作流程是什麼?您使用什麼方法啓動該服務?你也可以附上一些代碼。 – Blehi