在Android中,如何将字符串从活动传递到服务?

在Android中,如何将字符串从活动传递到服务?,android,Android,有人能告诉我们如何从活动到服务传递字符串或整数吗。 我正在尝试pas一个整数setPosition(4),但它不需要,当它启动时总是需要0个服务。我不知道为什么我不能使用服务实例从活动中进行操作 public class MainMP3 extends Activity{ Button play,stop,prev,next,list; static final String MEDIA_PATH = new String("/sdcard/"); Activity main_act

有人能告诉我们如何从活动到服务传递字符串或整数吗。 我正在尝试pas一个整数setPosition(4),但它不需要,当它启动时总是需要0个服务。我不知道为什么我不能使用服务实例从活动中进行操作

    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) {



        }
    }


        }

如果您只是在谈论将数据传递给
服务
,那么下面是我的解决方案。通过
Intent
启动服务时,将要传递给
服务的任何数据放入
捆绑包中,如下所示:

Bundle bundle = new Bundle();
bundle.putInt("my_Val", 4);
intent.putExtras(bundle);
Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
i.putExtra("incomingPhoneNumber", phoneNumber);
context.sendBroadcast(i);
然后像平常一样使用
意图
。另一方面,在您的
服务中
只需从
捆绑包中获取数据

Bundle bundle = intent.getExtras();
int myVal = bundle.getInt("my_Val", 0); //0 is just the default value used in case of error

您必须在intent中设置值,并在启动服务时传递该intent, 您可以在serivceonstartcommand(Intent-Intent、int-flags、int-startId)中获得值

这就是通过intent传递整数的方式

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()

  • 将此添加到
    onResume()

  • 在接收器/服务内部创建一个意图,如下所示启动广播:

    Bundle bundle = new Bundle();
    bundle.putInt("my_Val", 4);
    intent.putExtras(bundle);
    
    Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
    i.putExtra("incomingPhoneNumber", phoneNumber);
    context.sendBroadcast(i);
    

  • 就这样!祝你好运

    您正在创建ServiceConnection,但正在使用startService()API启动服务。考虑使用BDIN Service(),在重构您的代码之前,我强烈建议您阅读本文:服务中的意图是什么?它没有定义。嗨,你能检查一下吗?