Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的应用程序中的后退按钮有问题_Android_Button_Back - Fatal编程技术网

Android 我的应用程序中的后退按钮有问题

Android 我的应用程序中的后退按钮有问题,android,button,back,Android,Button,Back,我想在手机关机时清除共享首选项值?当设备关机时,如何清除共享首选项 您可以在设备启动时通过BraodcastReceiver将其清除 public class PhoneStateReceiver extends BroadcastReceiver{ @Override public void onReceive(final Context context, Intent intent) { if(intent.getAction().equals(Inten

我想在手机关机时清除共享首选项值?

当设备关机时,如何清除
共享首选项

您可以在设备启动时通过
BraodcastReceiver
将其清除

public class PhoneStateReceiver extends BroadcastReceiver{

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

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            //Clear your `SharedPreference` here.
        }
    }
}
在您的清单中添加以下内容:

<receiver android:name=".receiver.PhoneStateReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>  

添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


据我所知,唯一的可能性是使用
OnDestroy()
,但当设备关闭时,您的程序应该正在运行

与Android相同的问题:

我不知道有什么不同的方法。这个实现非常简单。只需通过调用SharedReference.Editor()上的.clear()来处理启动\u完成的广播操作和清除首选项

简单的启动接收器可能如下所示:

public class OnBootReceiver extends BroadcastReceiver{

                @Override
                public void onReceive(Context context, Intent intent) {
                      //clear preferences here         
                }

}
在您的AndroidManifest.xml中将其声明为:

           <receiver android:name=".OnBootReceiver">
                    <intent-filter>
                            <action android:name="android.intent.action.BOOT_COMPLETED" />
                    </intent-filter>
            </receiver>

您还需要获得以下权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


请改进您的问题,努力解决,否则问题将被关闭。再次提出相同的问题被视为滥用系统;请不要这样做。如果你能通过以下方式澄清你的问题,那么请这样做。一旦您的问题得到改进(请参阅了解方法中的本节),请提醒主持人注意。选择“其他”并要求修改以重新打开您的问题。