gpt4 book ai didi

java - 将变量从服务传递到 Activity

转载 作者:行者123 更新时间:2023-12-02 07:41:35 24 4
gpt4 key购买 nike

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

         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(),检查是否不为null,如果不为null则.getLong("accel1", 0);

如果您确信您之前所做的事情,那么这将会起作用。

关于java - 将变量从服务传递到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com