2

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

user2349990
  • 386
  • 4
  • 19

4 Answers4

2

save the variables in SharedPreferences

see how it works, 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

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.

Community
  • 1
  • 1
Darko Rodic
  • 1,010
  • 3
  • 10
  • 27
  • Thanks for the answer. I have edited my question to include anpther aspect. Could you please have a look and answer it accordingly? – user2349990 Jan 18 '14 at 10:37
  • Thank you.. I think this was what I was looking for.. Havent tried it yet but I'm hoping this would do the job. – user2349990 Jan 18 '14 at 10:57
1

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 .:-

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 :-

 <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>
Payal
  • 903
  • 1
  • 9
  • 28
0

Store your variable into SQLite or SharedPreferences. Then restore it's state on Activity restart.

Community
  • 1
  • 1
Maxim Korobov
  • 2,574
  • 1
  • 26
  • 44
  • Thanks for the answer. I have edited my question to include anpther aspect. Could you please have a look and answer it accordingly? – user2349990 Jan 18 '14 at 10:36
0

You can store your variables into sharedpreferences, this will be much easier

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);
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
  • Thanks for the answer. I have edited my question to include anpther aspect. Could you please have a look and answer it accordingly? – user2349990 Jan 18 '14 at 10:37
  • Do you want to restart your service without starting your app after reboot? – Orhan Obut Jan 18 '14 at 10:51
  • Yes.. I dont want the user to open my app. Instead immediately after reboot I want the service to be started if the service was enabled before reboot. – user2349990 Jan 18 '14 at 10:55