Android:自动启动应用程序和加载首选项

发布于 2024-09-01 06:03:17 字数 2257 浏览 4 评论 0原文

我在自动启动后正确初始化我的应用程序时遇到问题。

我已经设法让自动启动工作,重新启动后,应用程序显示为已启动,但计时器未启动。 我的猜测是,当我调用 context.startService() 时,不会调用 MyApp 的“onCreate”函数。计时器在 MyApp 的 doActivity() 函数中设置。

我将非常感谢任何有关我可能做错的事情的提示或优秀教程的链接。 :)

清单

    <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是一个BoradcastReciever,具有以下两个函数

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扩展了PreferenceActivity并具有onCreate()和doActivity(), doActivity() 读取首选项并根据它们设置计时器。

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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相权↑美人 2024-09-08 06:03:17

计时器在 doActivity() 中设置
MyApp的功能。

那永远不会起作用。 MyApp 是一项活动,只有在用户进入并启动它后才会创建。

onReceive() 中读取您的 SharedPreferences 并在那里设置闹钟。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文