判断GPS、网络是否开启
1.判断GPS打开与否,没有打开则打开GPS
private void initGPS(Context context) {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// 判断GPS模块是否开启,如果没有则开启
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("请打开GPS");
dialog.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
// 转到手机设置界面,用户设置GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 设置完成后返回到原来的界面
}
});
dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
dialog.show();
}
}
判断网络是否开启
private void initInternet(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnect = cm.getActiveNetworkInfo().isAvailable();
if (!isConnect) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("请打开网络或连接wifi");
dialog.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1) {
// 转到手机设置界面,用户设置GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 设置完成后返回到原来的界面
}
});
dialog.setNeutralButton("取消", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
dialog.show();
}
}