Android(安卓)Check GPS is Enabled or Not
16lz
2021-12-04
In android, we can easily check whether GPS is enabled in device or not using LocationManager.
Here is a simple program to Check.
GPS Enabled or Not :-
Add the below user permission line in AndroidManifest.xml to Access Location
1 | < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> |
Your java class file sgould be
public class ExampleApp extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); openGPSSettings(); } private void openGPSSettings() { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show(); }else{ showGPSDisabledAlertToUser(); } } private void showGPSDisabledAlertToUser() { // TODO Auto-generated method stub AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?") .setCancelable(false) .setPositiveButton("Goto Settings Page To Enable GPS", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ Intent callGPSSettingIntent = new Intent( android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(callGPSSettingIntent); } }); alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int id){ dialog.cancel(); } }); AlertDialog alert = alertDialogBuilder.create(); alert.show(); } }The output will looks like
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer