views:

759

answers:

1

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();
}
+1  A: 

The timers are set in the doActivity() function of MyApp.

That will never work. MyApp is an activity, one that will not be created until the user goes in and launches it.

Read your SharedPreferences in onReceive() and set the alarms there.

CommonsWare
Thanks! Good to know that. :)Trying to implement it I've come to the next problem. I add my preferences by using: addPreferencesFromResource(R.xml.preferences);I can't seem to get them named so that in the onRecieve() I can read them with: context.getSharedPreferences("prefs_myApp", Context.MODE_PRIVATE);I really want to create them from XML and not from code...
BBoom
Use `PreferenceManager.getDefaultSharedPreferences`.
CommonsWare
Awesome, thanks! You rock. :) Works perfectly now.(I'd vote you up if I had enough reputation :P)
BBoom