【发布时间】:2016-05-31 10:35:39
【问题描述】:
我有一个应用程序需要持续打开 gps 状态。假设在应用程序内,我关闭了我的 GPS。现在我希望我的应用程序显示再次启用 gps,否则它应该显示强制对话框。我希望你得到了这种情况.我不是在打开应用程序时询问,gps 打开或关闭。
【问题讨论】:
标签: android android-studio android-gps
我有一个应用程序需要持续打开 gps 状态。假设在应用程序内,我关闭了我的 GPS。现在我希望我的应用程序显示再次启用 gps,否则它应该显示强制对话框。我希望你得到了这种情况.我不是在打开应用程序时询问,gps 打开或关闭。
【问题讨论】:
标签: android android-studio android-gps
以下代码可能对您有所帮助..
声明广播接收者
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Log.e("GPS", "changed" + intent);
}
}}
在 AndroidManifest.xml 文件中声明
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
但是您已经声明了一个常量变量来监控 GPS Status 。在 GpsLocationReceiver 中,在 GPS 开启或关闭时进行更改,但不会检索开启或关闭值,因此您必须声明静态布尔变量来监控它,如果它关闭,则通过以下代码显示您的对话框以打开 GPS 设置。
public void showSettingsAlert(){
AlertDialog myAlertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
// Setting Dialog Title
builder.setTitle("GPS settings");
builder.setCancelable(false);
// Setting Dialog Message
builder.setMessage("For Use This Application You Must Need to Enable GPS. do you want to go Setting menu?");
// On pressing Settings button
builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
myAlertDialog = builder.create();
myAlertDialog.show();
}
【讨论】: