public static String getCityNameByGps() {
LocationManager lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false); // 不要求海拔信息
criteria.setBearingRequired(false); // 不要求方位信息
criteria.setCostAllowed(true); // 是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = lm.getBestProvider(criteria, true); // 获取GPS信息
Location location = lm.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateToNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
// lm.requestLocationUpdates(provider, 3000, 1, locationListener);
return addressString;
}
/**
*
* [功能描述].
*
* @param location
* [参数说明]
* @createTime 2011-10-11 下午03:22:21
*/
public static void updateToNewLocation(Location location) {
// System.out.println("begin updateLocation****");
if (location != null) {
// System.out.println("location is null");
double lat = location.getLatitude();
double lon = location.getLongitude();
// System.out.println("get lattitude from GPS ===>" + lat);
// System.out.println("get Longitude from GPS ===>" + lon);
// GeoPoint gPoint = new GeoPoint((int) lat, (int) lon);
StringBuilder sb = new StringBuilder();
Geocoder gc = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lon, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
// for (int i = 0; i < address.getMaxAddressLineIndex();
// i++) {
sb.append(address.getAddressLine(1)).append("\n");
// sb.append(address.getLocality()).append("\n");
// sb.append(address.getCountryName()).append("\n");
addressString = sb.toString();
// }
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
addressString = "未知地区";
}
}
// System.out.println("end updateLocation****");
}
/**
* 获取GPS状态
*
* @param context
* @return
*/
public static boolean getGPSState() {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// 判断GPS模块是否开启,如果没有则开启
if (!locationManager
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
return false;
} else {
return true;
}
}
public static void openGPS() {
LocationManager locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (!locationManager
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
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();
}
}
}
static Uri uri = Uri.parse("content://telephony/carriers");
static Context context;
public APNUtils(Context context){
this.context=context;
}
/**
* 检查网络
* @return
*/
public static boolean checkNet() {
boolean flag = false;
ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null) {
flag = cwjManager.getActiveNetworkInfo().isAvailable();
}
return flag;
}
/**
* 打开网络
*/
public static void openAPN() {
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchUtils.matchAPN(apn.apn));
cv.put("type", APNMatchUtils.matchAPN(apn.type));
context.getContentResolver().update(uri, cv, "_id=?",
new String[] { apn.id });
}
}
/**
* 关闭网络
*/
public static void closeAPN() {
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchUtils.matchAPN(apn.apn) + "mdev");
cv.put("type", APNMatchUtils.matchAPN(apn.type) + "mdev");
context.getContentResolver().update(uri, cv, "_id=?",
new String[] { apn.id });
}
}
private static List<APN> getAPNList() {
String tag = "Main.getAPNList()";
// current不为空表示可以使用的APN
String projection[] = { "_id,apn,type,current" };
Cursor cr = context.getContentResolver().query(uri, projection, null,
null, null);
List<APN> list = new ArrayList<APN>();
while (cr != null && cr.moveToNext()) {
Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + " "
+ cr.getString(cr.getColumnIndex("apn")) + " "
+ cr.getString(cr.getColumnIndex("type")) + " "
+ cr.getString(cr.getColumnIndex("current")));
APN a = new APN();
a.id = cr.getString(cr.getColumnIndex("_id"));
a.apn = cr.getString(cr.getColumnIndex("apn"));
a.type = cr.getString(cr.getColumnIndex("type"));
list.add(a);
}
if (cr != null)
cr.close();
return list;
}
private void showTips() { AlertDialog alertDialog = new AlertDialog.Builder(MainPageActivity.this) .setTitle("退出程序") .setMessage(" 是否退出程序?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MainPageActivity.this.finish(); onDestroy(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { return; } }).create(); // 创建对话框 alertDialog.show(); // 显示对话框 } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { this.showTips(); return false; } return false; } @Override protected void onDestroy() { super.onDestroy(); System.exit(0); }
2。
点击设定的退出程序按钮,提示是否退出:
// 退出 linear_out.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { linear_out.setBackgroundResource(R.drawable.more_font3); AlertDialog alertDialog = new AlertDialog.Builder( MoreMenuActivity.this).setTitle("退出程序").setMessage( " 是否退出程序?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MoreMenuActivity.this.finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { return; } }).create(); // 创建对话框 alertDialog.show(); // 显示对话框 } });