将变量从服务传递到活动

时间:2012-07-17 18:42:38

标签: java android android-activity

我正在尝试将服务类VandroidService2中的变量传递给我的活动类VandroidServiceClient: 以下是发送VandroidService2中的varible的代码:

         long accel = 8987; 
         Intent myIntent = new Intent(VandroidService2.this,VandroidServiceClient.class);
         myIntent.putExtra("accel1", accel);

以下是我在VandroidServiceClient中接收它的代码:

           TextView textView = (TextView) findViewById(R.id.textViewName);
           Intent mIntent = getIntent();
           long lovalue = mIntent.getLongExtra("accel1", 0);
           textView.setText(String.valueOf(lovalue));

由于某种原因,默认变量0显示在屏幕上?我做错了什么,是否需要更多信息来帮助我?

1 个答案:

答案 0 :(得分:2)

尝试intent.getExtras(),检查是否为空,如果不为空则为.getLong("accel1", 0);

如果您以前所做的 确定 ,这将有效。