从位于Android库中的AsyncTask发送数据到项目Android AppWidgetProvider

时间:2013-07-26 12:27:53

标签: android android-library

我有两个项目:Android主项目和Android库。在Android库中,我使用Asynctasks和来自onPostExecute的数据我希望能够发送到导入该库的android项目。在这种情况下,主项目是一个小部件应用程序,但我认为这应该不相关。 我想在我的主项目中使用onActivityResult,但这似乎是一个坏主意。

这就是我从主项目中调用我的库的方式:

 Intent libraryIntent = new Intent(context, ComplicatedRequest.class);
 libraryIntent.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(libraryIntent);

在我的库项目中,在onPostExecute方法中,我不知道如何将数据从那里发送回我的应用程序。我几乎肯定这是一个微不足道的问题,但我宁愿要求实施一个相当糟糕的实践解决方案。谢谢。

2 个答案:

答案 0 :(得分:0)

Use Intent and IntentFilter(Manifest)
<i>
 Intent intent = new Intent();
 intent.setAction(MY_ACTION);
 intent.put("key", value);
 context.startActivity(intent);
</i>

In Activity
<i>
 String s = intent.getExtra().getString("key");
 </i>

答案 1 :(得分:0)

使用startActivityForResult(参见http://developer.android.com/reference/android/app/Activity.html)(官方文档中的示例):

public class MyActivity extends Activity {
    static final int PICK_CONTACT_REQUEST = 0;

    protected boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            // When the user center presses, let them pick a contact.
            startActivityForResult(
                new Intent(Intent.ACTION_PICK, new Uri("content://contacts")), PICK_CONTACT_REQUEST);
            return true;
        }
        return false;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_CONTACT_REQUEST) {
            if (resultCode == RESULT_OK) {
                // A contact was picked.  Here we will just display it to the user.
                startActivity(new Intent(Intent.ACTION_VIEW, data));
            }
        }
    }
}

您可以通过data.getStringExtra(字符串名称)(或其他类型,如果需要)检索值。