使用OnCreate选项菜单进行Asynctask

时间:2014-03-14 07:58:36

标签: android android-actionbar

我有一个OnCreateActionMenu来分享我的价值观。我使用ShareContentprovider方法我通过intent传递了值。但问题是我想获取正在运行的值AsynTask。那么如果可以使用类似goto的声明呢?或任何其他方式? 我的代码是

public boolean onCreateOptionsMenu(Menu menu) {
    //getMenuInflater().inflate(R.menu.main, menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.share, menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultShareIntent());
    return super.onCreateOptionsMenu(menu);


}
private Intent getDefaultShareIntent() {
    // TODO Auto-generated method stub

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
 // passing the username
    intent.putExtra(Intent.EXTRA_SUBJECT,""+name+"via Myapp");
    // passing ID 
    intent.putExtra(Intent.EXTRA_TEXT,t1);
    return intent;

}

其中t1是textField

之后生成的AsynTask的值

2 个答案:

答案 0 :(得分:1)

当您引用mShareActionProvider对象时,您可以随时更改其共享意图。

您可以执行ASyncTask,完成后,您可以使用以下内容重新设置shareIntent

public boolean onPostExecute(String newString) {

    //newString here is the calculated String in ASyncTask
    mShareActionProvider.setShareIntent(getDefaultShareIntent(newString));

}

private Intent getDefaultShareIntent(String newText) {
    // TODO Auto-generated method stub

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    // passing the username
    intent.putExtra(Intent.EXTRA_SUBJECT,""+name+"via Myapp");
    // passing ID 
    intent.putExtra(Intent.EXTRA_TEXT,newText);
    return intent;
}

答案 1 :(得分:0)

AsyncTask有一个名为onProgressUpdate()的API。使用此功能,您可以将间歇性的progess值发送回UI线程。

protected void onProgressUpdate(Progress ... values)

在API级别3中添加 调用publishProgress(Progress ...)后在UI线程上运行。指定的值是传递给publishProgress(Progress ...)的值。

protected final void publishProgress(Progress ... values)

Added in API level 3
This method can be invoked from doInBackground(Params...) to publish updates on the UI thread while the background computation is still running. Each call to this method will trigger the execution of onProgressUpdate(Progress...) on the UI thread. onProgressUpdate(Progress...) will not be called if the task has been canceled.