在支持Android Cupcake(1.5)的设备上,如何检查和激活GPS?
当前回答
在LocationListener中,实现onProviderEnabled和onProviderDisabled事件处理程序。当你调用requestLocationUpdates(…)时,如果手机上的GPS是禁用的,onProviderDisabled将被调用;如果用户启用GPS, onProviderEnabled将被调用。
其他回答
是的,GPS设置不能再通过编程改变了,因为它们是隐私设置,我们必须检查它们是否从程序中打开,如果它们没有打开,我们就处理它。 你可以通知用户GPS被关闭,如果你愿意,可以使用类似这样的东西向用户显示设置屏幕。
检查位置提供程序是否可用
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
Log.v(TAG, " Location providers: "+provider);
//Start searching for location and update the location text when update available
startFetchingLocation();
}else{
// Notify users and show settings if they want to enable GPS
}
如果用户想启用GPS,你可以这样显示设置屏幕。
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE);
在onActivityResult中,你可以看到用户是否启用了它
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_CODE && resultCode == 0){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider != null){
Log.v(TAG, " Location providers: "+provider);
//Start searching for location and update the location text when update available.
// Do whatever you want
startFetchingLocation();
}else{
//Users did not switch on the GPS
}
}
}
这是一种方法,我希望它能有所帮助。 如果我做错了什么,请告诉我。
在android中,我们可以很容易地检查GPS是否在设备中启用或使用LocationManager。
这里有一个简单的程序来检查。
是否启用GPS:- 将AndroidManifest.xml中的以下用户权限行添加到访问位置
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您的java类文件应该是
public class ExampleApp extends Activity {
/** Called when the activity is first created. */
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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(){
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();
}
}
输出将如下所示
下面是在我的情况下工作的片段
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
`
以下是步骤:
步骤1:创建在后台运行的服务。
步骤2:在Manifest文件中也需要以下权限:
android.permission.ACCESS_FINE_LOCATION
步骤3:编写代码:
final LocationManager manager = (LocationManager)context.getSystemService (Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show();
第四步:或者你可以简单地使用:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
步骤5:持续运行服务以监控连接。
在LocationListener中,实现onProviderEnabled和onProviderDisabled事件处理程序。当你调用requestLocationUpdates(…)时,如果手机上的GPS是禁用的,onProviderDisabled将被调用;如果用户启用GPS, onProviderEnabled将被调用。
推荐文章
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?
- 如何确定Android设备的GPS是否已启用
- 没有找到与给定名称匹配的资源:attr 'android:keyboardNavigationCluster'。更新到支持库26.0.0时
- 禁用在android中打开导航抽屉的滑动手势
- Android:允许平板电脑竖屏和横屏,但强制手机竖屏?
- 如何使布局与视图填充剩余的空间?
- 字符串资源新行/n不可能?
- 如何点击或点击一个TextView文本
- Android房间-简单的选择查询-不能访问数据库在主线程
- Android: ScrollView vs NestedScrollView
- 错误:无法运行mksdcard SDK工具
- 未附加到窗口管理器的视图崩溃
- 将焦点设置为EditText
- 如何把空格字符放入XML字符串名?