Android-如何将数据从活动发送到服务? [英] Android- How to send data from activity to service?

查看:51
本文介绍了Android-如何将数据从活动发送到服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application I'm trying to send data from my MainActivity.class to a service called bgservice.class. This is my following code:

MainActivity.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);
}

bgservice.class:

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;

}

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

but I'm not getting the data in the service class. these are the following error I get:

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

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

java.lang.RuntimeException:无法启动活动
ComponentInfo {com.microapple.googleplace/com.microapple.googleplace.MainActivity}:java.lang.IllegalArgumentException:服务意图必须明确:意图{act = com.microapple.googleplace.bgservice(具有额外内容)}

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.microapple.googleplace/com.microapple.googleplace. MainActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.microapple.googleplace.bgservice (has extras) }

AndroidManifest.xml:

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>
....

推荐答案

此处:

 Intent serviceIntent = new Intent(bgservice.class.getName());

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

Passing String to Intent constructor for creating Intent to start Service. Intent constructor takes String as a Action name which we have added in AndroidManifest.xml.

<service android:enabled="true" android:name=".bgservice">
<intent-filter >
        <action android:name="com.microapple.googleplace.bgservice" />
    </intent-filter>
</service>

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

Now use com.microapple.googleplace.bgservice as action name to create Intent:

      Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice");
      serviceIntent.putExtra("UserID", "123456");
      this.startService(serviceIntent);

OR

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

Use Intent constrictor which takes Context as first parameter and component name which we want to start as second parameter :

  Intent serviceIntent = new Intent(this,bgservice.class);
  serviceIntent.putExtra("UserID", "123456");
  this.startService(serviceIntent);

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

And also use same key which using to add data in Intent currently adding value with UserID key but trying to get value using userID key

这篇关于Android-如何将数据从活动发送到服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆