【发布时间】:2010-10-25 00:39:10
【问题描述】:
在支持 Android Cupcake (1.5) 的设备上,如何检查和激活 GPS?
【问题讨论】:
-
接受答案怎么样? :)
标签: android gps android-sensors android-1.5-cupcake
在支持 Android Cupcake (1.5) 的设备上,如何检查和激活 GPS?
【问题讨论】:
标签: android gps android-sensors android-1.5-cupcake
最好的方法似乎如下:
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
【讨论】:
alert,以便您可以在 onDestroy 中将其关闭以避免内存泄漏 (if(alert != null) { alert.dismiss(); })
LocationManager.NETWORK_PROVIDER 将返回 true
在android中,我们可以使用LocationManager轻松检查设备是否启用了GPS。
这是一个简单的检查程序。
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();
}
}
输出看起来像
【讨论】:
alert,以便您可以在 onDestroy() 中将其关闭以避免内存泄漏 (if(alert != null) { alert.dismiss(); })
是的,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
}
}
}
这是一种方法,我希望它有所帮助。 如果我做错了什么,请告诉我。
【讨论】:
startActivityForResult 用于多个意图。当意图返回到您的活动时,您检查 RequestCode 以查看返回的意图并做出相应的响应。
provider 可以是空字符串。我不得不把支票改成(provider != null && !provider.isEmpty())
步骤如下:
第 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();
第 4 步: 或者您可以使用以下方法进行检查:
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
第 5 步:持续运行您的服务以监控连接。
【讨论】:
是的,您可以查看以下代码:
public boolean isGPSEnabled (Context mContext){
LocationManager locationManager = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
【讨论】:
此方法将使用 LocationManager 服务。
来源Link
//Check GPS Status true/false
public static boolean checkGPSStatus(Context context){
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return statusOfGPS;
};
【讨论】:
在 Kotlin 中:如何检查 GPS 是否启用
val manager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
checkGPSEnable()
}
private fun checkGPSEnable() {
val dialogBuilder = AlertDialog.Builder(this)
dialogBuilder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, id
->
startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))
})
.setNegativeButton("No", DialogInterface.OnClickListener { dialog, id ->
dialog.cancel()
})
val alert = dialogBuilder.create()
alert.show()
}
【讨论】:
这是我的案例中的 sn-p
final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
`
【讨论】:
如果用户允许在其设置中使用 GPS,则将使用 GPS。
你不能再明确地打开它,但你不必这样做 - 这确实是一个隐私设置,所以你不想调整它。如果用户对获得精确坐标的应用程序感到满意,它将启用。然后位置管理器 API 将尽可能使用 GPS。
如果您的应用在没有 GPS 的情况下确实没有用,并且已关闭,您可以使用 Intent 在右侧屏幕上打开设置应用,以便用户启用它。
【讨论】:
在您的LocationListener 中,实现onProviderEnabled 和onProviderDisabled 事件处理程序。当您拨打requestLocationUpdates(...)时,如果手机上禁用了GPS,将拨打onProviderDisabled;如果用户启用 GPS,将调用onProviderEnabled。
【讨论】:
Kotlin 解决方案:
private fun locationEnabled() : Boolean {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
【讨论】: