【问题标题】:IntentService - find number of Intents waiting in the queueIntentService - 查找队列中等待的 Intent 数量
【发布时间】:2015-04-14 15:34:24
【问题描述】:

在我的应用程序中,我使用 IntentService 来完成一些工作。我想找出有多少意图正在等待处理,因为 IntentService 将它们保存在“工作队列”中,并将下一个意图发送到 onStartCommand(),因为前一个意图的 onStartCommand 已经完成。

我怎样才能知道有多少 Intent 在这个“工作队列”中等待?

【问题讨论】:

    标签: android android-service android-intentservice


    【解决方案1】:

    其实很简单:你需要做的就是覆盖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
     }
    }
    

    【讨论】:

      【解决方案2】:

      使用SharedPreferences的解决方案:

      根据documentation,当IntentService收到启动请求时,系统调用onHandleIntent(Intent)

      因此,每当您向队列中添加 Intent 时,您都会递增并存储一个 Integer,它表示队列中 Intents 的数量:

      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();
      }
      

      最后,每当您想检查队列中有多少 Intents 时,您只需获取该值:

      public void checkQueue(){
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication());
          int numOfIntents = prefs.getInt("numOfIntents",0);
          Log.d("debug", numOfIntents);
      }
      

      【讨论】:

      • 问题是 onHandleIntent (你的意思是 onStartCommand 吗?)仅在“前一个”onHandleIntent 完成时才被调用。无论哪种方式,这都是一种 hack,有没有办法直接访问意图队列?另外,为什么要将整数存储在 sharedPreferences 中,而不仅仅是 IntentService 类中的私有(静态?)变量?
      • 我对此做了一些研究,但找不到任何“官方”的方式来实现你的目标。它可能是一个静态变量,但是您只能从 Service 类(我认为)访问它,使用 SharedPreference 您可以从整个应用程序中访问它。 @JonasCz
      • 我认为您可以将onHandleIntent 替换为onStartCommand
      • 好的,找到答案here。如果我将变量公开,我将能够访问可能应用程序中的任何地方,但无论如何我都不想要这个。
      • 这个答案的另一个问题:您共享的首选项数据将持续存在 - 例如,在设备重启时 - 而您的意图不会......
      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2012-05-06
      • 2011-10-31
      • 1970-01-01
      相关资源
      最近更新 更多