我该如何将活动中的字符串传递给Android中的服务?
问题描述:
任何人都可以告诉如何传递一个字符串或整数,无论从活动到服务。 im试图通过一个整数setpossition(4),但它并不需要,总是需要0服务,当它开始我不知道为什么我不能从活动通过使用服务的实例操作。我该如何将活动中的字符串传递给Android中的服务?
public class MainMP3 extends Activity{
Button play,stop,prev,next,list;
static final String MEDIA_PATH = new String("/sdcard/");
Activity main_activity;
@Override
public void onCreate(Bundle savedInstanceState) {
main_activity=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.mp3interface);
play= (Button) findViewById(R.id.play);
stop= (Button) findViewById(R.id.stop);
prev= (Button) findViewById(R.id.prev);
next= (Button) findViewById(R.id.next);
list= (Button) findViewById(R.id.listofsongs);
play.setOnClickListener(new StartClick());
stop.setOnClickListener(new StopClick());
prev.setOnClickListener(new PullClick());
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i= new Intent(MainMP3.this,songlist.class);
startActivity(i);
}
});
}
ServiceMP3 service = null;
ServiceConnection connection = new ServiceConnection() {
@Override // Called when connection is made
public void onServiceConnected(ComponentName cName, IBinder binder) {
service = ((ServiceMP3.SlowBinder)binder).getService();
}
@Override //
public void onServiceDisconnected(ComponentName cName) {
service = null;
}
};
private class StartClick implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(main_activity,ServiceMP3.class);
service.setposition(4);
main_activity.startService(intent);
}
}
private class StopClick implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(main_activity,ServiceMP3.class);
main_activity.stopService(intent);
}
}
private class PullClick implements View.OnClickListener {
public void onClick(View v) {
}
}
}
答
如果你只是在谈论将数据传递到您的Service
,那么这里是我的解决方案。当通过Intent
推出一个服务,把你想要传递给Service
在Bundle
任何数据,就像这样:
Bundle bundle = new Bundle();
bundle.putInt("my_Val", 4);
intent.putExtras(bundle);
然后使用Intent
正常人一样。在另一边,在你Service
距离Bundle
获取数据:
Bundle bundle = intent.getExtras();
int myVal = bundle.getInt("my_Val", 0); //0 is just the default value used in case of error
答
必须设置在意图上的价值和传递的意图,同时启动该服务, ,您可以在serivce获取值onStartCommand (Intent intent,int flags,int startId)。
这是你如何通过意图传递这个整数。
private class StartClick implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(main_activity,ServiceMP3.class);
intent.putExtra("position",4);
main_activity.startService(intent);
}
}
你可以得到的数值就是这个样子
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
int position = intent.getIntExtra("position", 0);
}
}
答
-
服务创建,在你想要得到你的数据到活动延伸
BroadcastReceiver
一个内部类:private BroadcastReceiver ReceivefromService = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { //get the data using the keys you entered at the service String IncomingSms=intent.getStringExtra("incomingSms");// String phoneNumber=intent.getStringExtra("incomingPhoneNumber"); } };
-
添加到
onPause()
:@Override protected void onPause() { super.onPause(); try { unregisterReceiver(ReceivefromService); } catch (IllegalArgumentException e) { if (e.getMessage().contains("Receiver not registered")) { Log.i("TAG","Tried to unregister the reciver when it's not registered"); } else { throw e; } } }
-
一下添加到
onResume()
:protected void onResume() { super.onResume(); filter.addAction("android.intent.action.SmsReceiver"); registerReceiver(ReceivefromService, filter); //the first parameter is the name of the inner class we created. }
-
接收器/服务,开始广播像这里面创建一个意图:
Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message); i.putExtra("incomingPhoneNumber", phoneNumber); context.sendBroadcast(i);
,这就是它!祝你好运!
您正在创建ServiceConnection,但使用startService()API启动该服务。考虑使用bindService(),并在重新构建代码之前,我强烈建议您阅读本文:http://developer.android.com/reference/android/app/Service.html#LocalServiceSample – 2012-02-21 01:37:33