【问题标题】:Constant GPS status on or off Verification on Android [closed]恒定的 GPS 状态打开或关闭 Android 上的验证 [关闭]
【发布时间】:2016-05-31 10:35:39
【问题描述】:

我有一个应用程序需要持续打开 gps 状态。假设在应用程序内,我关闭了我的 GPS。现在我希望我的应用程序显示再次启用 gps,否则它应该显示强制对话框。我希望你得到了这种情况.我不是在打开应用程序时询问,gps 打开或关闭。

【问题讨论】:

    标签: android android-studio android-gps


    【解决方案1】:

    以下代码可能对您有所帮助..

    声明广播接收者

    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();
    
    
    }
    

    【讨论】:

    • 你在哪里声明了静态变量?请发布完整的代码
    • 是的,请发布整个代码
    • Mainactivty 中的 Delcare 静态变量,如 public static Boolean GPS_STATUS ,并在 Receiver 中赋值
    • Dixit,那家伙要求提供完整的代码,如果你能通过将代码编辑到完整来帮助他,我将不胜感激
    • @Cyborg91 没有任何代码,但它是我检查 GPS 设置更改 Intent 的逻辑,由此我们可以检查其状态是否已更改
    猜你喜欢
    • 2023-03-18
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多