1

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

我的问题是一旦我关闭应用程序并重新打开它,我想从IntentService.

最好的方法是什么?

4

2 回答 2

3

您可以通过调用bindService()您的 Activity 让您的 Activity 绑定到您的服务。根据文档

当应用程序组件通过调用 bindService() 绑定到服务时,服务被“绑定”。绑定服务提供了一个客户端-服务器接口,允许组件与服务交互、发送请求、获取结果,甚至通过进程间通信 (IPC) 跨进程执行此操作。绑定服务仅在另一个应用程序组件绑定到它时运行。多个组件可以一次绑定到服务,但是当所有组件都解除绑定时,服务将被销毁。

还:

当您想通过应用程序中的活动和其他组件与服务交互或通过进程间通信 (IPC) 将应用程序的某些功能公开给其他应用程序时,您应该创建绑定服务。

该文档提供了一个完整的功能示例。以下摘自提供的链接。

服务等级:

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 可以调用的公共方法,例如轮询您的下载进度。详细解释请参考文档。

于 2015-03-20T09:03:19.973 回答
2

有几种方法可以在Service和之间建立通信连接Activity。我会推荐这2个

  • 首先,您可以使用出色的库Otto。使用 Otto,您还可以使用带@Produce注释的方法。使用此方法,您将返回有关下载的最新信息。当您@Subscribe进入时,您Activity将立即获得最新信息。https://github.com/square/otto

  • 如果您使用的是Android内置的,DownloadManager它会使用 . 返回更新和结果Broadcast,您可以Broadcast在您的ServiceActivity. 这样您就可以同时更新它们。我建议你使用DownloadManager,它很棒。 http://developer.android.com/reference/android/app/DownloadManager.html

于 2015-03-20T08:48:26.900 回答