Java 在Receiver类中使用上下文

Java 在Receiver类中使用上下文,java,android,android-intent,broadcastreceiver,android-context,Java,Android,Android Intent,Broadcastreceiver,Android Context,我有一个在设备打开时启动的应用程序。我试图做的是不打开任何活动,而是打开一个执行某些进程的线程 这是我的接收机等级: public class BOOTReceiver extends BroadcastReceiver { Info info = new Info(); public void onReceive(Context context, Intent intent) { assignUserInfo(context); SomeThread

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

这是我的接收机等级:

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();
以避免内存泄漏


希望能有所帮助。

正如pskink所说,我在文档中看到了以下注释:

从onReceive()返回后,BroadcastReceiver不再是 活动,并且它的宿主进程只与其他进程一样重要 在其中运行的应用程序组件

因此,在onReceive()函数中包含所有进程可以解决我的问题

然而,同样从文件中可以看出:

这意味着对于长时间运行的操作,您将经常使用 与BroadcastReceiver结合使用的服务,以保持包含 在整个操作过程中,进程处于活动状态

因此,正如Junior Buckeridge所说,使用服务也是执行更长操作的一种选择。

请阅读以下内容:“这段代码有什么问题?”:很难说,因为您没有告诉我们您的信息、某些线程或其他类是什么,或者它们是做什么的。