0

I have an app which requires a constant gps status ON.Suppose inside app,I turned off my GPS.Now I want my App to show to enable gps again or else it should show force dialog.I hope you got the situation.I am not asking during opening app,gps on or off.

4

1 回答 1

0

下面的代码可以帮助你..

声明广播接收器

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


}
于 2016-05-31T11:10:53.100 回答