java Android-如何将数据从活动发送到服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28710833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android- How to send data from activity to service?
提问by Bala u1
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
到名为bgservice.class
. 这是我的以下代码:
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):
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) }
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(有额外的)}
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>
....
采纳答案by ρяσ?ρ?я K
Here:
这里:
Intent serviceIntent = new Intent(bgservice.class.getName());
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
.
将 String 传递给Intent
构造函数以创建 Intent 以启动服务。Intent 构造函数将 String 作为我们在AndroidManifest.xml
.
<service android:enabled="true" android:name=".bgservice">
<intent-filter >
<action android:name="com.microapple.googleplace.bgservice" />
</intent-filter>
</service>
Now use com.microapple.googleplace.bgservice
as action name to create Intent:
现在com.microapple.googleplace.bgservice
用作操作名称来创建意图:
Intent serviceIntent = new Intent("com.microapple.googleplace.bgservice");
serviceIntent.putExtra("UserID", "123456");
this.startService(serviceIntent);
OR
或者
Use Intent constrictor which takes Context as first parameter and component name which we want to start as second parameter :
使用 Intent constrictor,它以 Context 作为第一个参数和我们想要作为第二个参数开始的组件名称:
Intent serviceIntent = new Intent(this,bgservice.class);
serviceIntent.putExtra("UserID", "123456");
this.startService(serviceIntent);
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
并且还使用与用于在 Intent 中添加数据的相同键,当前使用UserID
键添加值但尝试使用userID
键获取值
回答by Apurva
Intent serviceIntent = new Intent(YourActivity.this, bgservice.class);
serviceIntent.putExtra("UserID", "123456");
this.startService(serviceIntent);
And in your service,
在您的服务中,
public int onStartCommand(Intent intent, int flags, int startId) {
String userID = intent.getStringExtra("UserID");
//do something
return START_STICKY;
}
This must work.
这必须工作。