【问题标题】:Android- How to send data from activity to service?Android-如何将数据从活动发送到服务?
【发布时间】:2015-02-25 03:55:04
【问题描述】:

在我的应用程序中,我试图将数据从我的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);
}

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;

}

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

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

java.lang.RuntimeException:无法启动活动
组件信息{com.microapple.googleplace/com.microapple.googleplace. MainActivity}:java.lang.IllegalArgumentException:服务意图必须 明确:Intent { act=com.microapple.googleplace.bgservice (有 额外)}

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

【问题讨论】:

  • 您是否在清单中添加了该服务?
  • 是的,我添加了@RandykaYudhistira
  • @Balau1:你在清单中添加了com.microapple.googleplace.bgserviceAction吗?
  • @ρяσѕρєяK 你的意思是在android manifest.xml文件中添加
  • @Balau1 您正在发送“UserID”并接收“userID”,这没有任何意义。

标签: java android android-intent


【解决方案1】:
Intent serviceIntent = new Intent(YourActivity.this, bgservice.class);
        serviceIntent.putExtra("UserID", "123456");
        this.startService(serviceIntent);

为您服务,

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

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

    //do something

    return START_STICKY;

}

这必须有效。

【讨论】:

  • 如果我想在服务的 onCreate() 方法中使用 userID 变量怎么办?很抱歉评论 2015 年的答案。
  • 您可以在 onCreate() 中获取该变量为 Intent intent = getIntent();, if(intent != null &amp;&amp; intent.hasExtra("userID")){ String userID = intent.getExtra("userID") }
  • getIntent() 在服务中不起作用,那么我该如何发送可打包的数据
  • getIntent() 在 android 服务中工作。你能告诉我你是怎么用的吗?
【解决方案2】:

这里:

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

将字符串传递给Intent 构造函数以创建启动服务的意图。 Intent 构造函数将 String 作为我们在 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:

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

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

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

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

【讨论】:

  • @Balau1: 试试Intent serviceIntent = new Intent(this,bgservice.class);
  • @prosper k:有效!谢谢你帮助我。我改变了你在清单文件中所说的内容并使用了 Intent serviceIntent = new Intent(this,bgservice.class);我
  • @JerinAMathews:使用onStartCommand 方法从意图中获取数据。喜欢int UserID= intent.getIntExtra("UserID",0);
猜你喜欢
  • 2012-12-30
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多