开发者

How to pass data from android Service (from onStart or OnCreate) to android Activity

How to pass data from android Service (from onStart or OnCreate) to android Activity. I would like to pass data from inside the service using setResult(RESULT_OK,intent) to the sender (service开发者_Go百科 started activity) to OnActivityResult() method.


Do you start the activity from the service? if so why cant you just place the data inside the intent?

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("DataToBePassed", "some data here..");
startActivity(intent);

setResult is only when you start the activity via startActivityForResult and you set the result in the activity that is called. not on the service, the service is the one that will get the result.

do read http://developer.android.com/guide/topics/intents/intents-filters.html for more info.


You can use interface. Like

public interface YourCallback {
    public void onSuccess(String result);
}

Then use in anywhere .I used om my onPostExecute method.

yourCallback.onSuccess(result);

And use activity oncreate

new YourAsyncTask(new YourAsyncTas() {

     @Override
     public void onSuccess(String result) {
      yourGlobalFeald=result;

     }
    }).execute(if you want to use executable method);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜