如何通知我的主要活动,该线程已经完成?

时间:2014-11-03 21:26:50

标签: android multithreading android-intent android-activity android-asynctask

我需要做一件简单的事情:从我的应用中我必须制作一张照片,将其发送到服务器(在后台)并在发送后显示通知。一切正常,但现在我必须等待发送文件的结束 - 相机之后的活动正在关闭。我不想等待,我想在拍照后立即回到我的主要活动(但上传仍然会进入线程,并在完成后发送通知)。

问题:我不知道如何告知我的主要活动,该线程已完成上传照片。 也许将上下文或处理程序传递给相机活动会有所帮助,但我不能通过putExtra()来做到这一点。

有什么建议吗?

我的代码的一些片段:

MainActivity.java中的

Intent intent = new Intent(this, Camera.class);
startActivityForResult(intent, Constants.REQUESTCODE_CAMERA);
Camera.java:

中的

@Override
protected void onCreate(Bundle savedInstanceState) {
    //(...)
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    startActivityForResult(intent,TAKE_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //(...)
    new SendFileToServer().execute();
    // finish(); // I would like to finish Camera.java here, and get back to MainActivity, while SendFileToServer uploads file and send some notification later
}

protected class SendFileToServer extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... params){
        //(...) // here is sending of file to server, it works
    }

    @Override
    protected void onPostExecute(String result) {
        //(...) // code below doesn't work, because I didn't pass "context", I don't know how, or it's just impossible
        MainActivity mainActivity = (MainActivity) context;                 
        mainActivity.sendFileName(filename);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以做的是在AsyncTask的末尾发送广播,并在活动中注册您想要做某事的接收者(例如,显示通知)。

要做到这一点,你需要:

1)将应用程序上下文传递给AsyncTask(在Activity中):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //(...)
    new SendFileToServer(getApplicationContext()).execute(); // pass application context to AsyncTask
    finish();
}

2)从onPostExecute()发送广播:

protected class SendFileToServer extends AsyncTask<Void, Void, String>{

    private Context context;

    public SendFileToServer(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(Void... params){
        //(...) // here is sending of file to server, it works
    }

    @Override
    protected void onPostExecute(String result) {
        Intent intent = new Intent("com.example.UPLOAD_FINISHED");
        context.sendBroadcast(intent);
    }
}

3)在MainActivity中注册BroadcastReceiver(不要忘记在onPause()中取消注册接收者):

MainActivity.class

private BroadcastReceiver broadcastReceiver;

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(
            "com.example.UPLOAD_FINISHED");
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // show notification
        }
    };
    registerReceiver(broadcastReceiver, intentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
}