简体   繁体   中英

Control activity from a service

I'm coding an Android app, and I have to use websockets… I use 2 activities and a service: I use my main activity to take information on server, and I use a service to connect to this server. I want from this service to start the other activity, or let know to my main activity, when to start the other one. How can I do that ? Thanks for helping

You can do like this .

1.You must add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); for your Intent .

Else,you you will has

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

2.You can use intent.putExtra("key","value"); to pass data like Activity

Sample

Intent intent = new Intent(this, MainActivity.class);
// add flags
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("key","value");
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM