我有一个名为ForegroundService的服务类,在onstartCommand中我需要设置一个变量:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
VariableManaging.isStop=false; //<==variable i need to set. Here i got an exceptioninInitializer error when my bootReceiver start service on startup. If service is start by button on activity got no error.
}
VariableManaging类以:
开头static boolean isStop=true;
static ProgressDialog progressDialog=new ProgressDialog(Main.activity); //<== I noted that if i remove this all working well
有一种设置变量的方法吗?
答案 0 :(得分:0)
您的代码是正确的,但除非您像这样启动此服务,否则不会调用方法onStartCommand()
:
startService(new Intent(getApplicationContext(), ForegroundService.class));