持续开启或关闭 GPS 状态验证开启 Android

Constant GPS status on or off Verification on Android

我有一个应用程序需要在应用程序内保持恒定的 gps 状态 ON.Suppose,我关闭了我的 GPS.Now 我希望我的应用程序再次显示启用 gps,否则它应该显示强制 dialog.I 希望你得到 situation.I 我在打开应用程序时没有询问,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 状态。在 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();


}