开发者

Android: Autostart app and load preferences

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开发者_JAVA百科 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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜