Android GPS位置获取 为什么总是不能运行??!!!
到底哪里出了问题?请各位大侠们指点!不胜感激!
下面是源码:
MainActivity.java
package com.se7en;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.locationButtonId);
button.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View v){
LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
}
}
private class TestLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
System.out.println(location.getLongitude());
System.out.println(location.getLatitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
}
manifest 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.se7en"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Location1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/locationButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+string/绑定监听器"/>
</LinearLayout>
相关推荐
android使用GPS获取当前地理位置
第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对 象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation有2方面功能:1. 获取当前地理位置 2.如果当前位置定位不成功,则可以用此方法获取缓存中的上一次打开地图时定位的地理位置)。这样就避免了空指针异常。而且更重要的是GPS定位不是一下子就能定位成功的,在90%以上的情况下,getLastKnownLocation返回null 第二:LocationListener 最好在Activity的onCreate()方法中进行实例化 实现系统的回调方法: onLocationChanged(final Location loc) onProviderDisabled(final String s) onProviderEnabled(final String s) onStatusChanged(final String s, final int i, final Bundle b) 第三:requestLocationUpdates 必须要在onResume()中进行注册监听. 且在onPause()中进行反注 册。 第四:测试GPS是否定位成功,去一个空旷的地方去,不要有遮挡。这点非常重要,不然,你永远也不知道自己GPS定位是否成功。
android-GPS位置模拟精简代码自创可用
主要用于使用安卓手机模拟位置信息,如使用直接修改位置经纬度即可
Android原生GPS定位以及位置解析
Android原生GPS定位以及位置解析。 GPS致命缺点是室内无法使用,直接运行demo的同学注意第一次启动demo的时候,想要获取位置信息需要去室外,可以上下班路上做测试,首次获取位置信息的时间比较长。
Android6.0获取GPS定位和获取位置权限和位置信息的方法
今天小编就为大家分享一篇Android6.0获取GPS定位和获取位置权限和位置信息的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
android根据GPS获取经纬度和海拔
android根据GPS获取经纬度和海拔,还有速度和时间


