Android-如何将数据从活动发送到服务?

Bal*_* u1 9 java android android-intent

在我的应用程序中,我正在尝试将数据从我发送MainActivity.class到一个名为的服务bgservice.class.这是我的以下代码:

MainActivity.class:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Intent serviceIntent = new Intent(bgservice.class.getName());
  serviceIntent.putExtra("UserID", "123456");
  this.startService(serviceIntent);
}
Run Code Online (Sandbox Code Playgroud)

bgservice.class:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

  String userID = intent.getStringExtra("userID");
  System.out.println(userID);
  Toast.makeText(this,userID, Toast.LENGTH_LONG).show();
  return START_STICKY;

}
Run Code Online (Sandbox Code Playgroud)

但我没有在服务类中获取数据.这些是我得到的以下错误:

02-25 09:05:41.166:E/AndroidRuntime(2633):

java.lang.RuntimeException:无法启动活动
ComponentInfo {com.microapple.googleplace/com.microapple.googleplace.MainActivity}:java.lang.IllegalArgumentException:Service Intent必须是显式的:Intent {act = com.microapple.googleplace.bgservice(has extras)}

AndroidManifest.xml中:

     ...
    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >



        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:enabled="true" android:name=".bgservice" />
</application>
....
Run Code Online (Sandbox Code Playgroud)

Apu*_*rva 5

Intent serviceIntent = new Intent(YourActivity.this, bgservice.class);
        serviceIntent.putExtra("UserID", "123456");
        this.startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

为您服务

public int onStartCommand(Intent intent, int flags, int startId) {

    String userID = intent.getStringExtra("UserID");

    //do something

    return START_STICKY;

}
Run Code Online (Sandbox Code Playgroud)

这必须工作。


ρяσ*_*я K 3

这里:

 Intent serviceIntent = new Intent(bgservice.class.getName());
Run Code Online (Sandbox Code Playgroud)

将字符串传递给Intent构造函数以创建启动服务的意图。Intent 构造函数将 String 作为我们添加到 中的 Action 名称AndroidManifest.xml

<service android:enabled="true" android:name=".bgservice">
<intent-filter >
        <action android:name="com.microapple.googleplace.bgservice" />
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

现在使用com.microapple.googleplace.bgservice作为操作名称来创建 Intent:

      Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice");
      serviceIntent.putExtra("UserID", "123456");
      this.startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

或者

使用 Intent 收缩器,它将 Context 作为第一个参数,将我们要启动的组件名称作为第二个参数:

  Intent serviceIntent = new Intent(this,bgservice.class);
  serviceIntent.putExtra("UserID", "123456");
  this.startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

并且还使用相同的键,用于在 Intent 中添加数据,当前使用键添加值,但尝试使用键UserID获取值userID