[英]Error While calling service from activity
下面是我用來從我的活動中調用服務的代碼。 我想在進度條達到其最大值時調用該服務。 什么是正確的方法呢? 當我的進度條達到最大值然后我得到了錯誤....
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.
當您從服務中調用任何意圖時,您已設置了錯誤日志中提到的標記:
Intent intent = new Intent(mContext, "next activity.class"); //mContext is a Context variable.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
你試過這個:startService(new Intent(getApplicationContext(),MyService.class));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.