如何获取从调用活动传递的Android服务中的数据?使用意图将数据从活动传递到服务
回答
第一情境(可活动/服务等)
您有几种选择:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2)创建新套装
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
3)使用的意图
的 putExtra()快捷方法Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
新的上下文(可以是活动/服务等)
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(key);
}
注:捆绑有 “得” 与“放“所有原始类型的方法,Parcelables和Serializables。我只是将Strings用于示范目的。
对于准确的回答这个问题的“如何从活动中通过故意发送数据到服务”,就是你可以选择覆盖是你收到意图对象onStartCommand()
方法:
当您创建一个Service
应覆盖onStartCommand()
方法,所以如果你仔细看看下面的签名,这是你收到它传递给它的intent
对象:
public int onStartCommand(Intent intent, int flags, int startId)
从您将创建我的活动
所以ntent对象启动服务,然后你把你的数据的意图对象里面,比如你想传递一个UserID
从Activity
到Service
:
Intent serviceIntent = new Intent(YourService.class.getName())
serviceIntent.putExtra("UserID", "123456");
context.startService(serviceIntent);
当服务在该方法中开始了它的onStartCommand()
方法将被调用,所以您可以从意向对象检索值(用户名),例如
public int onStartCommand (Intent intent, int flags, int startId) {
String userID = intent.getStringExtra("UserID");
return START_STICKY;
}
注:以上答案指定要获取与getIntent()
方法的意图这是不正确的服务上下文
这应该是被接受的答案。服务接受的答案是错误的。 – zeeshan 2014-07-06 00:40:32
我有这个错误:'无法启动服务意图:找不到' – fullOfQuestion 2016-07-30 12:07:50
如果你绑定你的服务,你会得到额外的onBind(Intent intent)
。
活动:
Intent intent = new Intent(this, LocationService.class);
intent.putExtra("tour_name", mTourName);
bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
服务:
@Override
public IBinder onBind(Intent intent) {
mTourName = intent.getStringExtra("tour_name");
return mBinder;
}
这是否适用于系统服务? – 2017-02-17 19:23:47
@ChefPharaoh这是一个很好的问题。尝试记录意图的值。 'Arrays.toString(yourAry [])'会帮助你。 – 2017-06-08 18:45:48
因为我想要传递一个自定义类,我想通过实现可分区界面,这一切都很好。不过谢谢。 – 2017-06-08 20:52:40
另一个posibility使用意图。的getAction:
在服务:
public class SampleService inherits Service{
static final String ACTION_START = "com.yourcompany.yourapp.SampleService.ACTION_START";
static final String ACTION_DO_SOMETHING_1 = "com.yourcompany.yourapp.SampleService.DO_SOMETHING_1";
static final String ACTION_DO_SOMETHING_2 = "com.yourcompany.yourapp.SampleService.DO_SOMETHING_2";
static final String ACTION_STOP_SERVICE = "com.yourcompany.yourapp.SampleService.STOP_SERVICE";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
//System.out.println("ACTION: "+action);
switch (action){
case ACTION_START:
startingService(intent.getIntExtra("valueStart",0));
break;
case ACTION_DO_SOMETHING_1:
int value1,value2;
value1=intent.getIntExtra("value1",0);
value2=intent.getIntExtra("value2",0);
doSomething1(value1,value2);
break;
case ACTION_DO_SOMETHING_2:
value1=intent.getIntExtra("value1",0);
value2=intent.getIntExtra("value2",0);
doSomething2(value1,value2);
break;
case ACTION_STOP_SERVICE:
stopService();
break;
}
return START_STICKY;
}
public void startingService(int value){
//calling when start
}
public void doSomething1(int value1, int value2){
//...
}
public void doSomething2(int value1, int value2){
//...
}
public void stopService(){
//...destroy/release objects
stopself();
}
}
在活动时间:
public void startService(int value){
Intent myIntent = new Intent(SampleService.ACTION_START);
myIntent.putExtra("valueStart",value);
startService(myIntent);
}
public void serviceDoSomething1(int value1, int value2){
Intent myIntent = new Intent(SampleService.ACTION_DO_SOMETHING_1);
myIntent.putExtra("value1",value1);
myIntent.putExtra("value2",value2);
startService(myIntent);
}
public void serviceDoSomething2(int value1, int value2){
Intent myIntent = new Intent(SampleService.ACTION_DO_SOMETHING_2);
myIntent.putExtra("value1",value1);
myIntent.putExtra("value2",value2);
startService(myIntent);
}
public void endService(){
Intent myIntent = new Intent(SampleService.STOP_SERVICE);
startService(myIntent);
}
最后,清单文件:
<service android:name=".SampleService">
<intent-filter>
<action android:name="com.yourcompany.yourapp.SampleService.ACTION_START"/>
<action android:name="com.yourcompany.yourapp.SampleService.DO_SOMETHING_1"/>
<action android:name="com.yourcompany.yourapp.SampleService.DO_SOMETHING_2"/>
<action android:name="com.yourcompany.yourapp.SampleService.STOP_SERVICE"/>
</intent-filter>
</service>
您正在多次启动该服务......是否意味着每次都会创建同一服务的多个实例? – oshurmamadov 2017-05-05 05:57:33
服务具有单例模式。 http://stackoverflow.com/questions/2518238/does-startservice-create-a-new-service-instance-or-using-the-existing-one – 2017-05-06 11:31:57
“switch(action)”action can be null – 2018-01-16 09:24:13
活动:
int number = 5;
Intent i = new Intent(this, MyService.class);
i.putExtra("MyNumber", number);
startService(i);
服务:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getExtras() != null){
int number = intent.getIntExtra("MyNumber", 0);
}
}
服务:startservice可引致影响,使用Messenger和传递数据最好的办法。
private CallBackHandler mServiceHandler= new CallBackHandler(this);
private Messenger mServiceMessenger=null;
//flag with which the activity sends the data to service
private static final int DO_SOMETHING=1;
private static class CallBackHandler extends android.os.Handler {
private final WeakReference<Service> mService;
public CallBackHandler(Service service) {
mService= new WeakReference<Service>(service);
}
public void handleMessage(Message msg) {
//Log.d("CallBackHandler","Msg::"+msg);
if(DO_SOMETHING==msg.arg1)
mSoftKeyService.get().dosomthing()
}
}
活动:从意向获取Messenger的填充它传递数据和传递消息发回给服务
private Messenger mServiceMessenger;
@Override
protected void onCreate(Bundle savedInstanceState) {
mServiceMessenger = (Messenger)extras.getParcelable("myHandler");
}
private void sendDatatoService(String data){
Intent serviceIntent= new
Intent(BaseActivity.this,Service.class);
Message msg = Message.obtain();
msg.obj =data;
msg.arg1=Service.DO_SOMETHING;
mServiceMessenger.send(msg);
}
- 1. 将数据从意图服务传递到活动
- 2. 将数据从意图传递到片段到活动
- 3. 如何将数据动态地从活动传递到服务?
- 4. 从活动传递意图到服务失败
- 5. 将数据从服务传递到当前活动
- 6. Android:如何将数据从活动传递到服务?
- 7. 无法将数据从活动传递到服务
- 8. 将数据从线程服务传递到活动
- 9. 将数据从活动传递到服务
- 10. 将数据从服务传递到活动
- 11. 将数据传回活动从服务
- 12. 将数据从AccessibilityService传递到活动
- 13. 将数据传递到由服务启动的活动
- 14. 无法将位图从活动传递到AIDL服务
- 15. 从活动传递意图数据到类
- 16. 安卓数据使用意图传递给另一个活动
- 17. 将数据从一个活动传递到另一个活动
- 18. 将数据从tabhost活动传递到其内部活动
- 19. 将数据从新活动传递到旧活动?
- 20. 将意图传递给活动?
- 21. 如何将意图从TabActivity传递到其子活动
- 22. 使用意向传递数据到三个活动
- 23. 将数据从活动传递到其视图
- 24. 将数据从视图传递到活动
- 25. 将数据从服务传输到活动的最佳实践
- 26. 如何通过意图将数据从服务传递到Android中的BroadcastReceiver
- 27. 如何将基于时间的变化数据从活动传递到服务
- 28. Android如何将实时数据从本地服务传递到活动堆栈?
- 29. 从活动传递数据到BroadcastReceiver
- 30. 将数据从服务传递到另一个服务
但是,我们不能用getIntent()方法的服务中。当我们从活动向服务发送价值时,如何实现这一点? – 2013-04-07 04:02:00
如何获得int值... – Prakhar 2013-08-15 07:22:31
这对服务不起作用...? – 2014-04-05 00:59:45