Java Android Studio-如何将数据从MainActivity.xml发送到服务?

Java Android Studio-如何将数据从MainActivity.xml发送到服务?,java,android,android-studio,Java,Android,Android Studio,在创建应用程序时,我遇到了一个问题。我有很多复选框,每个复选框负责打开/关闭特定的声音。我使用MediaPlayer提供了服务,但我不知道如何将复选框值“checked”从MainActivity.xml发送到该服务 MainActivity.xml: super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, MySer

在创建应用程序时,我遇到了一个问题。我有很多复选框,每个复选框负责打开/关闭特定的声音。我使用MediaPlayer提供了服务,但我不知道如何将复选框值“checked”从MainActivity.xml发送到该服务

MainActivity.xml:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, MyService.class));

    CheckBox soundCheckBox = (CheckBox)findViewById(R.id.sound1cb);
    CheckBox sound2CheckBox = (CheckBox)findViewById(R.id.sound2cb);
    CheckBox sound3CheckBox = (CheckBox)findViewById(R.id.sound3cb);
    CheckBox sound4CheckBox = (CheckBox)findViewById(R.id.sound4cb);
    CheckBox sound5CheckBox = (CheckBox)findViewById(R.id.sound5cb);

添加您的意向数据

intent.putExtra("key" , "value");

因为您想要从一个活动到另一个服务(可能还有其他方式)进行通信,所以需要使用。这将允许您在活动中获取服务绑定器,然后调用服务方法。。。这是一个很大的简化,但我认为深入研究这一点会解决您的问题。

您必须首先了解这一点

startService(new Intent(activity, SampleService.class).putExtra("key", "value"))
并将这一行添加到onCreate或onStartCommand methud中获取数据的服务中

intent.getStringExtra("key")