简体   繁体   English

Android:重启手机后恢复应用程序变量

[英]Android: Resume app variables after a phone reboot

I have developed an application which makes use of a toggle button to decide the enabling/disabling of my app. 我已经开发了一个应用程序,该应用程序使用切换按钮来决定启用/禁用我的应用程序。 I store the toggle button state as a static variable so that it retains it value on stop and resume. 我将切换按钮状态存储为静态变量,以使其在停止和恢复时保持其值。 However on a reboot, the static variables will be reinitialised into its default state. 但是,在重新启动时,静态变量将重新初始化为默认状态。 Is there any way I can get my app to resume its state even after a reboot? 有什么方法即使重启后也能使我的应用恢复其状态?

Specifically, what my app does is that on toggle On it enables a "Service". 具体来说,我的应用程序执行的操作是在启用后启用“服务”。 SO I want that service to be started automatically on a phone reboot. 因此,我希望该服务在电话重启时自动启动。 Is that possible? 那可能吗?

Thanks 谢谢

save the variables in SharedPreferences 将变量保存在SharedPreferences

see how it works, http://developer.android.com/reference/android/content/SharedPreferences.html 查看其工作原理, http://developer.android.com/reference/android/content/SharedPreferences.html

then on start of your activity just restore them 然后在您开始活动时将其还原

not so important but if youre using toggle you may save boolean 不是很重要,但是如果您使用切换功能,则可以保存布尔值

tutorial: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html 教程: http//www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html

GL GL

EDIT: on other Q about services, well dont really know what you made so far but maybe this: Android -Starting Service at Boot Time can help, if not provide some code what you made. 编辑:关于服务的其他问题,可能真的不知道您到目前为止所做的一切,但是也许这样: Android-在启动时启动服务可以提供帮助,如果没有提供您所做的一些代码。

Well,that's possible when you your app starts as the phone reboots.Service will be called via main activity. 好吧,这是有可能的,当您的应用程序在电话重启时启动时。服务将通过主要活动调用。 You can start the app as soon as the phone boots via a broadcastrecievir which looks like this .:- 您可以通过如下所示的broadcastrecievir在电话启动后立即启动该应用程序。

public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);  
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

}

This needs to be added in AndroidManifest as :- 这需要在AndroidManifest中添加为:-

 <receiver
        android:name="com.example.xyz.BootUpReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Store your variable into SQLite or SharedPreferences . 将变量存储到SQLiteSharedPreferences Then restore it's state on Activity restart. 然后在“ Activity重新启动”时恢复其状态。

You can store your variables into sharedpreferences, this will be much easier 您可以将变量存储到sharedpreferences中,这将更加容易

http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/reference/android/content/SharedPreferences.html

// to save it
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("your_key", "your_value");
editor.commit();


//to retrieve it back

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String your_value= prefs.getString("your_key", null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM