在网上找了各个版本的GPS定位,可是都不能运行!要么是Sorry,the application has stopped unexpectly.Please try agian.要么就是在DDMS send经纬度之后毫无反应……哭……
到底哪里出了问题?请各位大侠们指点!不胜感激!
下面是源码:
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>
到底哪里出了问题?请各位大侠们指点!不胜感激!
下面是源码:
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>
我已经在DDMS中发送经纬度了,可是还是无法定位,显示location=null。而且发送经纬度之后,模拟器就会死机重启
怎么知道模拟器不支持GPS功能?
我刚开始也是那样,不过后来解决了,是设计模式的问题,要符合android的设计模式才好,版本越高对这个要求就越高,
我现在碰见的问题是在2.3.3真机上老是获取不到location,不知道是手机的问题还是.....?
是在2.3.3上不能运行。但我在真机上(2.3.6)可以获得location~你看看是不是程序有问题……