开发者

Android: passing text from an app to another one

In my Android project, I launch an application from another one, by using an intent, in this way:

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("edu.dhbw.andar.sample","edu.dhbw.andar.sample.CustomActivity"));
        startActivity(intent);

And it works: the second application starts without problems. Now, I wish to pass a text string from the first application to the seco开发者_如何学编程nd one. Is there a way to do that?

Thanks


Put an extra String in the intent (using intent#putExtra()) and read it in the new application:

In the first application:

intent.putExtra("IdentifierForYourString", theString);

In the second:

getIntent().getStringExtra("IdentifierForYourString");


Put the string in the intent

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("edu.dhbw.andar.sample","edu.dhbw.andar.sample.CustomActivity"));
        intent.putExtra("Key", myString);
        startActivity(intent);

Then in your new activity you do:

getParent().getActivity().getIntent().getExtras().getString("Key");

to get shipped string.


You can use Bundles to send extra information from one activity to another, i.e.:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new

ComponentName("edu.dhbw.andar.sample","edu.dhbw.andar.sample.CustomActivity"));

intent.putExtra("myparameter", "myvalue");

startActivity(intent);

In the targeted activity you can retrieve the extra information like:

protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    Bundle extras = getIntent().getExtras();

    Log.d("LOG", extras.getString("myparameter");
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜