在activity中: Intent service=new Intent(CarActivity.this, GetLocation.class);// 设置服务将要跳转
 service.putExtra("CAR_ID", userid);// 将userid传递给CAR_ID在service中: public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId); 
    carID= intent.getStringExtra("CAR_ID");
System.out.println("Get args from intent:" + carID); }这样,在logcat中能正常输出,但是,如果将System.out.println("Get args from intent:" + carID);放在其他地方,打印Get args from intent:  null
大侠们帮忙啊。。

解决方案 »

  1.   

    activity 向 service传值,在onStart(Intent intent, int startId){}中接收这个值,但是接收到这个值,不能应用于全局变量,只能在onStart()方法中用。
      

  2.   

    你确定你的userid是String类型?如果不是String类型肯定取不到。
      

  3.   


    你是不是在onCreate,或者onCreate调用的函数理使用了这个全局变量?
    onCreate是在onStart之前运行的,如果是这样,你取不到变量很正常。