2013-01-20 24 views
-1

我有一個名爲ForegroundService服務類onstartCommand,我需要設置一個變量:Android的 - 不能設置在服務類中的變量

@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

是什麼?Main.activity它是一個靜態變量嗎? – KhalidTaha

回答

0

你的代碼是正確的,但除非你啓動此服務這樣的方法onStartCommand()將不會被調用:

startService(new Intent(getApplicationContext(), ForegroundService.class)); 
+0

不要認爲這是問題。我開始這樣的服務: 意圖intent =新的意圖(ForegroundService.ACTION_FOREGROUND); intent.setClass(context,ForegroundService.class); context.startService(intent); –

+0

Main.activity是什麼?你傳遞靜態上下文嗎? – Jambaaz