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

时间:2012-02-20 23:42:10

标签: android

任何人都可以告诉如何传递一个字符串或整数,从活动到服务。 我试图通过一个整数setpossition(4),但它不需要,它始终需要0服务启动我不知道为什么我不能通过使用服务实例从Activity操纵。

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



        }
    }


        }

3 个答案:

答案 0 :(得分:4)

您必须在intent中设置值并在启动服务时传递该intent, 你可以在服务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);
       }
    }

答案 1 :(得分:1)

如果您只是在谈论将数据传递到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

答案 2 :(得分:0)

  1. 创建一个内部类,在您希望获取数据的活动中扩展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");
    
        }
    };
    
  2. 将此添加到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;
            }
        }
    }
    
  3. 将此添加到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.
    }
    
  4. 在接收器/服务中创建一个启动广播的意图,如下所示:

    Intent i = new Intent("android.intent.action.SmsReceiver").putExtra("incomingSms", message);
    i.putExtra("incomingPhoneNumber", phoneNumber);
    context.sendBroadcast(i);
    
  5. 就是这样!古德勒克!