android intent

Author: yifei / Created: July 6, 2017, 7:40 p.m. / Modified: July 6, 2017, 7:41 p.m. / Edit

You should use intent to communicate between activities and services

intent extra

package data from activity

create a intent instance

Intent intent = new Intent(getBaseContext(), SignoutActivity.class); intent.putExtra("key", value); startActivity(intent);

package data from service

Intent intent = new Intent(context, SignoutActivity.class); intent.putExtra("key", value); startService(intent);

Access data in activity

Access that intent on next activity String s = getIntent().getStringExtra("EXTRA_SESSION_ID"); The docs for Intents has more information (look at the section titled "Extras").

Access data in Service

just use the Intent parameter from onStartCommand

public int onStartCommand (Intent intent, int flags, int startId) { String userID = intent.getStringExtra("UserID"); return START_STICKY; }

intent plags


intent action

有任何问题可以发邮件到 kongyifei (at) 讨论