Java 将变量从服务传递到活动
我正在尝试将服务类VandroidService2中的变量传递给活动类VandroidServiceClient: 以下是发送VandRoidService 2中变量的代码:Java 将变量从服务传递到活动,java,android,android-activity,Java,Android,Android Activity,我正在尝试将服务类VandroidService2中的变量传递给活动类VandroidServiceClient: 以下是发送VandRoidService 2中变量的代码: long accel = 8987; Intent myIntent = new Intent(VandroidService2.this,VandroidServiceClient.class); myIntent.putExtra("accel1", accel)
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显示在屏幕上?我做错了什么,需要更多的信息来帮助我吗?尝试intent.getExtras()
,如果不为空,请检查.getLong(“accel1”,0)代码>
如果你确信你以前所做的一切,这将起作用。看起来它应该起作用。。。我假设您使用startActivity(myIntent)代码>在VandroidService2中?我使用了这个。startActivity(myIntent)所说的是:minent.getExtras();如果(minent.getExtras()!=null){long lovalue=minent.getLong(“accel1”,0);textView.setText(String.valueOf(lovalue));//}minent.getExtras().getLong(“name”,defaultvalue)代码>