我如何获得内从一个调用活动通过一个机器人服务的数据?
How do I get data within an Android Service that was passed from an invoking Activity?
第一上下文(可以是活动/服务等)
您有几种选择:
1)使用包从的意图:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2)创建一个新的捆绑
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
3)使用putExtra()该意向书的快捷方式
3) Use the putExtra() shortcut method of the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
新上下文(可以是活动/服务等)
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(key);
}
注意:捆绑有得与放的方法对所有的基本类型,parcelables的,和Serializables。我只是用字符串示范的目的。的
NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.