Android:单击按钮时访问gps功能

时间:2013-12-05 03:40:55

标签: java android xml button gps

我已经创建了一个返回gps坐标的函数,它到目前为止工作得很好,但我希望它只在单击一个按钮时启动。我已经尝试将gps合并到单击按钮时启动的事件所描述的功能中,但是当我这样做时我遇到了很多错误,而且我对如何解决这个问题没有任何其他想法。

这是.java文件:(Saver.java)

package com.example.lifesaver;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Saver extends Activity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saver);
b = (Button) findViewById(R.id.button1);

/* b.setOnClickListener(new OnClickListener(){ 
  public void onClick(View v)
  {
      localizare();
  }
}); */


 // We use LocationManager class to obtain GPS locations
LocationManager mlocManager =   (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

//MyLocationListener class

public class MyLocationListener implements LocationListener
{
//  private void localizare()
//  {
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "Current location is: " + "Latitud = " + loc.getLatitude() +   "Longitud = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    }

    public void onProviderDisabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Enabled",  Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}//}
}

和.xml文件(活动)saver.xml):

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Saver" >

<Button
    android:id="@+id/button1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/circle" 
    android:onClick="onClick"/>

</RelativeLayout>

另外,我在AndroidManifest.xml文件中添加了这个:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>

2 个答案:

答案 0 :(得分:0)

您需要做的是让听众自己工作,但当位置发生变化时,什么都不做,只需将位置信息保存在字段中。
然后,将侦听器添加到按钮。当用户对该按钮进行陈词滥调时,您将在监听器中读取该字段,并执行您需要的任何操作(例如示例中的Toast)。因此,最后一个已知位置始终在newLocation字段中可用,但仅在按下按钮时才会读取

这是代码:

public class Saver extends Activity {

    Button b;

    Location newLocation = null;//add thiss

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saver);
        b = (Button) findViewById(R.id.button1);


             //set this listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (newLocation != null) {

                    String Text = "Current location is: " + "Latitud = "
                            + newLocation.getLatitude() + "Longitud = "
                            + newLocation.getLongitude();
                    Toast.makeText(getApplicationContext(), Text,
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        // We use LocationManager class to obtain GPS locations
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                mlocListener);
    }

    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location loc) {

               //save the new location
               newLocation = loc;

        }

        public void onProviderDisabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String provider) {
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
}

答案 1 :(得分:0)

我添加一个标志“isGpsOn”。

package com.example.lifesaver;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Saver extends Activity {

Button b;
boolean isGpsOn  = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saver);
b = (Button) findViewById(R.id.button1);

 b.setOnClickListener(new OnClickListener(){ 
  public void onClick(View v)
  {
      isGpsOn = true;
  }
}); 


 // We use LocationManager class to obtain GPS locations
LocationManager mlocManager =   (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

//MyLocationListener class

public class MyLocationListener implements LocationListener
{
//  private void localizare()
//  {
    public void onLocationChanged(Location loc)
    {
        if (isGpsOn)
        {
            loc.getLatitude();
            loc.getLongitude();
            String Text = "Current location is: " + "Latitud = " + loc.getLatitude() +   "Longitud = " + loc.getLongitude();
            Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
        }
    }

    public void onProviderDisabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Enabled",  Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}//}
}