TextView值未被服务更新 - Android GPS

时间:2016-11-23 21:09:03

标签: android gps android-gps

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

MainActivity:

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>

1 个答案:

答案 0 :(得分:0)

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

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

显然,您的distance始终为0.0