【问题标题】:Interaction between IntentService and Activity - AndroidIntentService 和 Activity 之间的交互 ​​- Android
【发布时间】:2015-03-20 07:52:35
【问题描述】:

在我的应用程序中,我使用IntentService 从云端下载文件。并在NotificationManager 中显示进度。我需要在Activity 中显示状态(下载/完成或失败),它也盯着IntentService

我的问题是,一旦我关闭应用再打开,我想从IntentService获取下载状态。

最好的方法是什么?

【问题讨论】:

  • 创建一个私有类并扩展ResultReceiver。谷歌它的教程。

标签: android android-download-manager android-intentservice


【解决方案1】:

您可以通过在您的 Activity 中调用 bindService() 来让您的 Activity 绑定到您的服务。根据documentation

当应用程序组件通过以下方式绑定到服务时,服务被“绑定” 调用 bindService()。绑定服务提供客户端-服务器 允许组件与服务交互的接口,发送 请求,获得结果,甚至跨进程这样做 进程间通信(IPC)。绑定服务仅运行 另一个应用程序组件绑定到它。多个组件可以 立即绑定到服务,但当所有这些都解除绑定时,服务 被销毁了。

还有:

当您想与 来自应用程序中的活动和其他组件的服务,或者 将您的应用程序的某些功能公开给其他应用程序, 通过进程间通信 (IPC)。

The documentation 提供了一个完整的功能示例。以下摘自提供的链接。

服务类:

public class LocalService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();
    // Random number generator
    private final Random mGenerator = new Random();

    /**
     * Class used for the client Binder.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with IPC.
     */
    public class LocalBinder extends Binder {
        LocalService getService() {
            // Return this instance of LocalService so clients can call public methods
            return LocalService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public int getRandomNumber() {
      return mGenerator.nextInt(100);
    }
}

活动类:

public class BindingActivity extends Activity {
    LocalService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to LocalService
        Intent intent = new Intent(this, LocalService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    /** Called when a button is clicked (the button in the layout file attaches to
      * this method with the android:onClick attribute) */
    public void onButtonClick(View v) {
        if (mBound) {
            // Call a method from the LocalService.
            // However, if this call were something that might hang, then this request should
            // occur in a separate thread to avoid slowing down the activity performance.
            int num = mService.getRandomNumber();
            Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

在您的 Service 中,您可以定义您的 Activity 可以调用的公共方法,例如轮询您的下载进度。详细解释请参考文档。

【讨论】:

  • 问题说明 IntentService,这个答案没有解决这个问题。否决
  • @Clocker oooo savage!
  • @Marcus lmao 野蛮人!这让我崩溃了:D
【解决方案2】:

ServiceActivity 之间有几种通信连接方式。我会建议这些 2

【讨论】:

  • 为什么不简单的让Activity绑定到服务上呢?
  • 绑定通常对每个人来说都不是那么简单。清单可以很长。我真的很喜欢奥托。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2013-07-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2012-09-06
  • 2016-11-11
相关资源
最近更新 更多