和activity中互相传值类似,本例子为传递double类数据
在activity中
Intent intent=new Intent();
intent.setClass(MainActivity.this, GPSService.class);
intent.putExtra("des_lat", 27.9326830000);
intent.putExtra("des_log",120.7167670000);
startService(intent);
然后再service中的onStart函数中获取该值
gpsSer.des_lat=intent.getDoubleExtra("des_lat", 0.00);
gpsSer.des_log=intent.getDoubleExtra("des_log", 0.00);
System.out.print("-------------"+gpsSer.des_lat+gpsSer.des_log);
当然写到这里还是不能传的,不然会报错!!!
我们需要在Mainfeist文件中注册这个service
本文档介绍如何在Android中通过Intent从Activity向Service传递double类型的数据。示例代码展示了如何设置Intent,启动Service,并在Service的onStart函数中正确获取额外的double值。别忘了在Manifest文件中注册Service,否则将导致运行时错误。
4277

被折叠的 条评论
为什么被折叠?



