-1

我在我的 gps 上使用广播接收器来检查用户是否在应用程序工作时禁用了 GPS。如果用户禁用,则显示一个对话框,其中包含需要 GPS 的信息。

这是我在清单中的示例:

    <receiver android:name=".BroadCastGPS">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

这是我的广播 GPS 课程:

public class BroadCastGPS extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                if(!gpsStatus.GPS) {

                    Intent pushintent = new Intent(context, gpsStatus.class);
                    pushintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(pushintent);

                }

            } else {

                if(gpsStatus.GPS) {

                    Intent pushintent = new Intent(context, Online.class);
                    pushintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(pushintent);

                }

            }
        }

    }
}

我的问题是,当用户关闭此应用程序时,几秒钟后显示信息对话框,需要 GPS。如何在关闭应用程序之前停止广播?我尝试使用 System.exit(0),杀死进程但它不起作用。

此示例在关闭应用程序时调用:

PackageManager pm = mContext.getPackageManager();
ComponentName compName =
        new ComponentName(mContext,
                BroadCastGPS.class);
pm.setComponentEnabledSetting(
        compName,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);
4

2 回答 2

1

切勿尝试强制关闭您的应用程序。Android 会处理这个问题。如果您想关闭您的组件,请使用setComponentEnabledSetting

于 2016-03-10T08:24:18.643 回答
0

只需在用户禁用 gps 时取消注册广播接收器即可。

 unregisterReceiver(YOUR_BROADCAST);
于 2016-03-10T08:26:46.870 回答