IntentService - 查找队列中等待的 Intents 数量
IntentService - find number of Intents waiting in the queue
在我的应用程序中,我使用 IntentService 来完成一些工作。我想知道有多少意向正在等待处理,因为 IntentService 将它们保存在 'work queue' 中,并在前一个 onStartCommand
完成时将下一个发送到 onStartCommand()
.
我怎样才能知道有多少 Intent 在这个 'work queue' 中等待?
使用SharedPreferences的解决方案:
根据documentation,系统在IntentService
收到启动请求时调用onHandleIntent(Intent)
。
因此,无论何时将 Intent
添加到队列中,都会递增并存储一个 Integer
,它将代表队列中 Intent
的数量:
public void addIntent(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int numOfIntents = prefs.getInt("numOfIntents", 0);
numOfIntents++;
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("numOfIntents",numOfIntents);
edit.commit();
}
然后,每次调用 onHandleIntent(Intent)
时,您都会减少 Integer
值:
public void removeIntent(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int numOfIntents = prefs.getInt("numOfIntents", 0);
numOfIntents--;
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("numOfIntents",numOfIntents);
edit.commit();
}
最后,每当您想检查队列中有多少 Intent
时,只需获取该值即可:
public void checkQueue(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
int numOfIntents = prefs.getInt("numOfIntents",0);
Log.d("debug", numOfIntents);
}
实际上这很简单:您需要做的就是覆盖 onStartCommand(...)
并递增一个变量,然后在 onHandleIntent(...)
中递减它。
public class MyService extends IntentService {
private int waitingIntentCount = 0;
public MyService() {
super("MyService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
waitingIntentCount++;
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onHandleIntent(Intent intent) {
waitingIntentCount--;
//do what you need to do, the waitingIntentCount variable contains
//the number of waiting intents
}
}
在我的应用程序中,我使用 IntentService 来完成一些工作。我想知道有多少意向正在等待处理,因为 IntentService 将它们保存在 'work queue' 中,并在前一个 onStartCommand
完成时将下一个发送到 onStartCommand()
.
我怎样才能知道有多少 Intent 在这个 'work queue' 中等待?
使用SharedPreferences的解决方案:
根据documentation,系统在IntentService
收到启动请求时调用onHandleIntent(Intent)
。
因此,无论何时将 Intent
添加到队列中,都会递增并存储一个 Integer
,它将代表队列中 Intent
的数量:
public void addIntent(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int numOfIntents = prefs.getInt("numOfIntents", 0);
numOfIntents++;
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("numOfIntents",numOfIntents);
edit.commit();
}
然后,每次调用 onHandleIntent(Intent)
时,您都会减少 Integer
值:
public void removeIntent(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int numOfIntents = prefs.getInt("numOfIntents", 0);
numOfIntents--;
SharedPreferences.Editor edit = prefs.edit();
edit.putInt("numOfIntents",numOfIntents);
edit.commit();
}
最后,每当您想检查队列中有多少 Intent
时,只需获取该值即可:
public void checkQueue(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
int numOfIntents = prefs.getInt("numOfIntents",0);
Log.d("debug", numOfIntents);
}
实际上这很简单:您需要做的就是覆盖 onStartCommand(...)
并递增一个变量,然后在 onHandleIntent(...)
中递减它。
public class MyService extends IntentService {
private int waitingIntentCount = 0;
public MyService() {
super("MyService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
waitingIntentCount++;
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onHandleIntent(Intent intent) {
waitingIntentCount--;
//do what you need to do, the waitingIntentCount variable contains
//the number of waiting intents
}
}