【问题标题】:TextView value not being updated by the Service - Android GPS服务未更新 TextView 值 - Android GPS
【发布时间】:2016-11-23 21:09:03
【问题描述】:

我有一个按钮可以启动我的 DelayedMessageService。该服务是非常简单的服务,因为它所做的只是计算用户行进的距离。我想显示行驶的总距离,但问题是它没有在我的 MainAcitivy 的 TextView 中显示值。有人可以帮我解决这个问题。这是我的 MainAcvity、Service 和 activity_main.xml:

主活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    String distanceStr;

    DelayedMessageService mDelayedMessageService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        watchMileage();



        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, DelayedMessageService.class);
                intent.putExtra(DelayedMessageService.EXTRA_MESSAGE,
                        getResources().getString(R.string.button_response));
                startService(intent);
            }
        });
    }

    private void watchMileage() {
        final TextView distanceView = (TextView)findViewById(R.id.distance);
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                double distance = 0.0;
                if (mDelayedMessageService != null) {
                    distance = mDelayedMessageService.getMiles();
                }
                 distanceStr = String.format("%1$,.2f miles", distance);
                distanceView.setText(distanceStr);
                handler.postDelayed(this, 1000);
            }
        });
    }

    //Log.v("INOCRECTEA", distanceStr.toString());
}

//activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context=".MainActivity">

  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/button_text"
      android:id="@+id/button"
      android:onClick="onClick"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />

  <TextView android:text=""
            android:id="@+id/distance"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:singleLine="false"
            android:textSize="50sp"/>
</LinearLayout>

【问题讨论】:

    标签: android gps android-gps


    【解决方案1】:

    您声明了一个名为 mDelayedMessageService 的变量,但我没有看到您在您提供的代码的 sn-p 中对其进行了初始化,因此它是 null。然后在watchMileage() 里面做

    if (mDelayedMessageService != null) {
          distance = mDelayedMessageService.getMiles();
    }
    

    显然,您的distance 将永远是0.0

    【讨论】:

    • 感谢@Renat Kaitmazov。我究竟在哪里初始化 mDelayedMessageService 以使其工作?我是否在 watchMileage() 中写入 mDelayedMessageService = new DelayedMessageService () 我认为这段代码已经启动并初始化 DelayedMessageService Intent intent = new Intent(MainActivity.this, DelayedMessageService.class); intent.putExtra(DelayedMessageService.EXTRA_MESSAGE, getResources().getString(R.string.button_response));启动服务(意图);
    • 没有。您不应该自己初始化服务。它是一个系统组件,系统本身为您完成所有工作。在这里,您通过startService() 方法使用了所谓的启动服务。为了获得对服务的引用,您需要一个所谓的绑定服务。这是文档developer.android.com/guide/components/bound-services.html
    • 还有一件事需要注意。只有在用户单击按钮后才会触发实际的距离计算。在计算发生之前,您可以直接在 onCreate() 内调用 watchMileage()。当用户单击该按钮时,您需要存储结果,以便下次显示。
    • 我认为绑定服务只会在您的活动处于前台时运行您的服务。即使我的活动不存在,我也想在后台运行我的服务。我该怎么做?
    • 是的,你是对的。绑定服务在活动处于前台时工作。但这是获取服务引用的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多