6

所以这是我的 MyLocationListener 类

package com.example.gpslocater;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MyLocationListener implements LocationListener {

        public TextView mLocationTextView;
    Context mContent;
    public MyLocationListener(Context context) {
        mContent = context;
    }
    @Override
    public void onLocationChanged(Location location) {

        location.getLatitude();
        location.getLongitude();

        String Text = "My current location is :" + 
        "Latitude = " + location.getLatitude() +
        " Longitude = " + location.getLongitude();
        //figure out a way to make it display through a text view
        mLocationTextView.setText(Text);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(mContent, "Gps Disabled", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(mContent, "Gps Enabled", Toast.LENGTH_SHORT);


    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

这是我的 GPS 活动课程

public class GPSActivity extends Activity {
    private Button mGPSButton;
    private TextView mLatitudeTextView;
    private TextView mLongitudeTextView;
    private LocationManager mLocationManager;
    private LocationListener mLocationListener;

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

        mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new MyLocationListener(null);
        mGPSButton = (Button)findViewById(R.id.press_button);
        mGPSButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.g, menu);
        return true;
    }

}

现在当我运行它时它可以工作但是在我按下按钮后程序停止响应有很多错误消息 一只忙碌的猫

有一条错误消息说 java.lang.SecurityException "gps" 位置提供程序需要 ACCESS_FINE_LOCATION 权限,如果需要,我是否必须添加它,我应该在哪里添加它?我认为这是我的程序崩溃的原因。

4

5 回答 5

11

从提供的 logcat 中理解。将此添加到您的清单中

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
于 2013-05-22T12:15:28.147 回答
2

您必须向 Android 清单添加以下权限:

AndroidManifest.xml

...

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

...

您还可以使用 Eclipse 的权限编辑器。

于 2013-05-22T12:36:26.377 回答
0

在清单文件中添加以下权限

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>// missing in manifest file

一些有用的链接

权限列表

http://developer.android.com/reference/android/Manifest.permission.html

清单文件

http://developer.android.com/guide/topics/manifest/manifest-intro.html

于 2013-05-22T12:15:34.320 回答
0

把它放在你的清单中.. 许可

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
于 2013-05-22T12:17:37.710 回答
-1

是的,您需要使用许可

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
于 2013-05-22T12:16:26.220 回答