Android: Autostart app and load preferences

Posted by BBoom on Stack Overflow See other posts from Stack Overflow or by BBoom
Published on 2010-05-12T10:12:16Z Indexed on 2010/05/12 11:14 UTC
Read the original article Hit count: 248

Filed under:
|

Hi, I have a problem with initializing my app properly after the autostart.

I've managed to get an autostart to work, after a reboot the app is shown as started but the timer's are not. My guess is that the "onCreate" function of MyApp is not called when I call the context.startService(). The timers are set in the doActivity() function of MyApp.

I would greatly appreciate any tips on what I could be doing wrong or links to good tutorials. :)

The manifest:

    <activity android:name=".MyApp"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="MyApp_Receiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>[/syntax]

MyApp_Receiver is a BoradcastReciever with the following two functions

public void onReceive(Context context, Intent intent) {
    // Do Autostart if intent is "BOOT_COMPLETED"
    if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
    {
        // Start the service
        context.startService(new Intent(context, MyApp.class));
    }
    // Else do activity
    else
        MAIN_ACTIVITY.doActivity();
}

public static void setMainActivity(MyApp activity)
{
    MAIN_ACTIVITY = activity;
}

MyApp extends PreferenceActivity and has an onCreate() and a doActivity(), the doActivity() reads out the preferences and sets a timer depending on them.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Show preferences
    addPreferencesFromResource(R.xml.preferences);;

    // Register Preference Click Listeners
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);


    // Prepare for one-shot alarms
    if (mIntent == null)
    {
        mIntent = new Intent(MyApp.this, MyApp_Receiver.class);
        mSender = PendingIntent.getBroadcast(MyApp.this,
                0, mIntent, 0);
        MyApp_Receiver.setMainActivity(this);
    }

    // Refresh and set all timers on start
    doActivity();
}

© Stack Overflow or respective owner

Related posts about android

Related posts about autostart