2011-03-15 15 views
0

調用服務時的錯誤是我用來從我的活動調用服務的代碼。 我想在進度條達到最大值時調用該服務。什麼纔是正確的做法呢?我的進度條達到最大值,然後我得到錯誤....以下是從活動

Handler progressHandler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
      if (dialog.getProgress() == dialog.getMax()) 
      { 

       //Log.d(TAG, "onClick: stopping srvice"); 

       // stopService(new Intent(oddg.this, MyService.class)); 

       Log.d(TAG, "onClick: starting service"); 
       startService(new Intent(oddg.this, MyService.class)); 
      } 

      dialog.incrementProgressBy(increment); 
     } 
    }; 

日誌:

ERROR/AndroidRuntime(725): java.lang.RuntimeException: Unable to create service org.androidpeople.dialog.MyService: android.util.AndroidRuntimeException: 
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. 
+0

這是什麼錯誤? – Reno 2011-03-15 06:32:21

+0

錯誤/ AndroidRuntime(725):java.lang.RuntimeException:無法創建服務org.androidpeople.dialog.MyService:android.util.AndroidRuntimeException:從Activity上下文外調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。這是我在日誌文件中得到的錯誤...... – Smith 2011-03-15 06:35:52

+0

@reno告訴我要做什麼? – Smith 2011-03-15 06:47:52

回答

2

當您從您設置標誌,你在你的錯誤都提到一個服務調用任何意圖日誌:

Intent intent = new Intent(mContext, "next activity.class"); //mContext is a Context variable. 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
mContext.startActivity(intent); 
+0

嗨...實際上是從活動中調用服務..... – Smith 2011-03-15 07:15:30

+0

您給:this.startService(intent); – Mathew 2011-03-15 07:50:26

+0

我想打電話給我的服務類那是你寫在那裏......是那個錯誤????請告訴我 – Smith 2011-03-15 09:09:43

0

你試試這個: startService(新的意向書(getApplicationContext(),MyService.class));

相關問題