android intent

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

see http://stackoverflow.com/questions/21833402/difference-between-intent-flag-activity-clear-task-and-intent-flag-activity-task

# intent action

About 逸飞

后端工程师

发表评论

电子邮件地址不会被公开。 必填项已用*标注