GPS
1.构建一个GPS的测试的Activity,实时获取 经度和纬度(可找出附近的人儿)
public class GPS_testActivity extends Activity
{
/** Called when the activity is first created. */
private LinearLayout mainView = null;
private TextView infoView = null;
private TextView locationView = null;
private LocationManager locationManager = null;
private LocationListener locationListener = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mainView = new LinearLayout(this);
mainView.setOrientation(LinearLayout.VERTICAL);
infoView = new TextView(this);
mainView.addView(infoView);
locationView = new TextView(this);
mainView.addView(locationView);
setContentView(mainView);
locationManager_init();
}
/* locationManager初始化 */
void locationManager_init()
{
locationManager = (LocationManager) this
.getSystemService(GPS_testActivity.LOCATION_SERVICE);
locationListener_init();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
}
/* locatonListener初始化 */
void locationListener_init()
{
locationListener = new LocationListener()
{
// 位置变化时触发
public void onLocationChanged(Location location)
{
System.out.println("onLocationChanged");
locationView.setText("时间:" + location.getTime() + "\n");
locationView.append("经度:" + location.getLongitude() + "\n");
locationView.append("纬度:" + location.getLatitude() + "\n");
locationView.append("海拔:" + location.getAltitude() + "\n");
}
// gps禁用时触发
public void onProviderDisabled(String provider)
{
System.out.println("onProviderDisabled");
Toast.makeText(GPS_testActivity.this, "请开启GPS!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
}
// gps开启时触发
public void onProviderEnabled(String provider)
{
Toast.makeText(GPS_testActivity.this, "GPS正常",
Toast.LENGTH_SHORT).show();
}
// gps状态变化时触发
public void onStatusChanged(String provider, int status,
Bundle extras)
{
System.out.println("onStatusChanged");
if (status == LocationProvider.AVAILABLE)
{
infoView.setText("当前GPS状态:可见的\n");
} else if (status == LocationProvider.OUT_OF_SERVICE)
{
infoView.setText("当前GPS状态:服务区外\n");
} else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
infoView.setText("当前GPS状态:暂停服务\n");
}
}
};
}
}
说明:客户端得到gps数据传给服务器端处理。。最后显示出服务返回的数据 搞定!用户看起来很神奇的功能,在代码里并不复杂。不过服务器端处理数据稍微麻烦些,需要根据经纬度找出你附近的人儿
2.android设置gps自动开启方法总结
1.第一种方法
private void toggleGPS()
{
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try
{
PendingIntent.getBroadcast(StartActivity.this, 0, gpsIntent, 0)
.send();
} catch (CanceledException e)
{
e.printStackTrace();
}
}
2.第二种方法
private void openGPSSettings()
{
//获取GPS现在的状态(打开或是关闭状态)
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER );
if(gpsEnabled)
{
//关闭GPS
Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, false );
}
else
{
//打开GPS
Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, true);
}
3.第三种方法(手动设置)
LocationManager alm = (LocationManager)StartActivity.this.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
说明: 第一第二种需要加上权限
<!--允许程序读取或写入系统设置 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
3. android程序自动开启GPS功能完整代码实例
/**
* GPS设置类
*
* @author hedandan
*
*/
public class SettingsGPS
{
/**
* 开启GPS模式
*/
public static void openGPS(Context context)
{
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
context.getContentResolver(), LocationManager.GPS_PROVIDER);
if (!gpsEnabled)
{
toggleGPS(context);
Toast.makeText(context, "GPS模式成功开启!", Toast.LENGTH_SHORT).show();
}
}
/**
* 关闭GPS模式
*/
public static void closeGPS(Context context)
{
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
context.getContentResolver(), LocationManager.GPS_PROVIDER);
if (gpsEnabled)
{
toggleGPS(context);
Toast.makeText(context, "GPS模式成功关闭!", Toast.LENGTH_SHORT).show();
}
}
/**
* GPS模式开关
*/
public static void toggleGPS(Context context)
{
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try
{
PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();
} catch (CanceledException e)
{
e.printStackTrace();
}
}
}
说明:这是一个接口工具类,在android程序中只需调用此接口就可以实现GPS功能的自动开启与关闭