Swa*_*oid 1 android android-intent android-service
我想将字符串从片段传递到服务.我试过setvalue和bind,但它适用于活动而不是startservice对吗?什么是"ServiceConnection",通过使用ServiceConnection可以传递字符串?这是我的片段代码来启动服务.
**Solved **
Run Code Online (Sandbox Code Playgroud)
我已将我的代码更改为此,并且它完美无缺
Intent intent = new Intent(getActivity(), myPlayService.class);
Bundle b = new Bundle();
b.putString("link", "http://94.23.154/bbc");
intent.putExtras(b);
getActivity().startService(intent);
Run Code Online (Sandbox Code Playgroud)
我在使用中
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if(intent != null){
Bundle bundle = intent.getExtras();
link = bundle.getString("link");
}
Run Code Online (Sandbox Code Playgroud)
您可以通过Intent将字符串从Fragment传递到Service并使用以下putExtra()方法:
Intent intent = new Intent(getActivity(), myPlayService.class));
intent.putExtra("string param 1", "String for the Service");
getActivity().startService(intent);
Run Code Online (Sandbox Code Playgroud)
在服务中,您将检索字符串onStartCommand():
public int onStartCommand(Intent intent, int flags, int startId) {
String stringFromFragment = intent.getStringExtra("string param 1");
// TODO do something with the string
startPlayer();
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2427 次 |
| 最近记录: |