错误在呼吁从服务活动 [英] Error While calling service from activity

查看:166
本文介绍了错误在呼吁从服务活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code我用从我的活动调用服务。
我想,当进度条达到最大值来调用服务。这将是这样做的正确方法?我的进度条达到最大值,然后我得到错误....

 处理程序progressHandler =新的处理程序()
    {
        公共无效的handleMessage(消息MSG)
        {
            如果(dialog.getProgress()== dialog.getMax())
            {                //Log.d(TAG的onClick:停止srvice);                // stopService(新意图(oddg.this,MyService.class));                Log.d(TAG的onClick:启动服务);
                startService(新意图(oddg.this,MyService.class));
            }            dialog.incrementProgressBy(增量);
        }
    };

日志:

  ERROR / AndroidRuntime(725):了java.lang.RuntimeException:无法创建服务org.androidpeople.dialog.MyService:android.util.AndroidRuntimeException:
从活动上下文之外调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。


解决方案

当你从设置标志作为你在错误日志中提到一个服务调用任何意图:

 意向意图=新意图(mContext,下一步activity.class); // mContext是一个上下文变量。
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(意向);

below is the code I use to call a service from my activity. I want to call the service when the progress bar reaches its max value. what would be the right way to do it? as my progress bar reached to max then i got error....

    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);
        }
    };

Logs:

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. 

解决方案

When you call any intent from a service you have set flag as you have mentioned in your error log:

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

这篇关于错误在呼吁从服务活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆