下面是我用来从我的活动中调用服务的代码。 我想在进度条达到其最大值时调用该服务。什么是正确的方法呢?当我的进度条达到最大值然后我得到错误....
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 :(得分:2)
当你从服务中调用任何意图时,你已经设置了你在错误日志中提到的标志:
Intent intent = new Intent(mContext, "next activity.class"); //mContext is a Context variable.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
答案 1 :(得分:0)
你试过这个: startService(new Intent(getApplicationContext(),MyService.class));