gpt4 book ai didi

java - 在接收器类中使用上下文

转载 作者:行者123 更新时间:2023-12-01 12:35:56 25 4
gpt4 key购买 nike

我有一个在设备打开时启动的应用程序。我尝试做的不是打开任何 Activity ,而是打开一个执行某些进程的线程。

这是我的接收器类:

public class BOOTReceiver extends BroadcastReceiver {
Info info = new Info();

public void onReceive(Context context, Intent intent) {
assignUserInfo(context);
SomeThread u = new SomeThread(info);
u.run();
}

private void assignUserInfo(Context ctx) {
info.setInfo(AnotherClass.getInfo(ctx));
}
}

如果我调用“assignUserInfo”,它使用参数“context”调用另一个类,则应用程序不会启动。否则,线程正在工作。

这段代码有什么问题?

最佳答案

我认为你应该使用 IntentService 来进行线程处理。另外,如果您的应用程序可以唤醒手机,您应该尝试从 WakefulBroadcastReceiver 扩展接收器。作为附加建议,请尽可能尝试使用应用程序上下文

context.getApplicationContext();

避免内存泄漏。

http://developer.android.com/reference/android/app/IntentService.html http://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

希望有帮助。

关于java - 在接收器类中使用上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25593835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com