How to use “goAsync” for broadcastReceiver?

前端 未结 2 1008
忘掉有多难
忘掉有多难 2020-11-30 06:00

Background

Starting with Honeycomb (API 11) , Android has a feature to allow the broadcastReceiver run in an async way, providing it about 10 seconds before it ass

2条回答
  •  一生所求
    2020-11-30 06:30

    You can find short explanation here.

    Use goAsync() if you want to handoff the processing inside of your BroadcastReceiver's onReceive() method to another thread. The onReceive() method can then be finished there. The PendingResult is passed to the new thread and you have to call PendingResult.finish() to actually inform the system that this receiver can be recycled.

    For example:

    final PendingResult result = goAsync();
    Thread thread = new Thread() {
       public void run() {
          int i;
          // Do processing
          result.setResultCode(i);
          result.finish();
       }
    };
    thread.start();
    

提交回复
热议问题