启动Service报错:Service Intent must be explicit

  直奔主题,看一下错误代码:

Intent intent=new Intent("com.demo.aidlservice");
bindService(intent,mServiceConnection,Context.BIND_AUTO_CREATE);

  
运行之后直接崩溃,报错信息是Service Intent must be explicit,必须显示指定Intent,运行环境android5.0。照例先看
官方文档和我们说了一些什么:

  文档中说明了这里Intent的作用是指明要连接的Service,提到了两个条件:

1.指定组件的名称

2.指定一个合理的描述,例如action,category等等,使得可以匹配service的IntentFilter

  改为如下所示即可正常运行:

Intent intent = new Intent();
intent.setAction("com.demo.aidlservice");
intent.setPackage("com.demo.aldlserver");bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

  可以见得,google在安全方面给开发者的限制正在变得越来越多,5.0中的必须显示启动Service,废弃getRunningTasks方法,6.0中的运行时权限,包括7.0中取消的一些系统广播,无一不让我们的系统更加健壮,更加安全,也让一些app厂家少钻一些空子。接着刨根究底看一下源码吧,看一下这个异常时如何抛出来的。我们都知道Context只是一个抽象类,定义了一些抽象方法由ContextImpl来实现具体逻辑。手头没有Framework源码的兄弟可以在这个网站在线查看http://androidxref.com。我这里看的是5.0的framework。

@Override
public boolean bindService(Intent service, ServiceConnection conn,int flags) {
    warnIfCallingFromSystemProcess();
    return bindServiceCommon(service, conn, flags, Process.myUserHandle());
}

  bindService方法调用了bindServiceCommon方法,追进去看一下:

        private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
1762            UserHandle user) {
1763        IServiceConnection sd;
1764        if (conn == null) {
1765            throw new IllegalArgumentException("connection is null");
1766        }
1767        if (mPackageInfo != null) {
1768            sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1769                    mMainThread.getHandler(), flags);
1770        } else {
1771            throw new RuntimeException("Not supported in system context");
1772        }
1773        validateServiceIntent(service);
1774        try {
1775            IBinder token = getActivityToken();
1776            if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1777                    && mPackageInfo.getApplicationInfo().targetSdkVersion
1778                    < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1779                flags |= BIND_WAIVE_PRIORITY;
1780            }
1781            service.prepareToLeaveProcess();
1782            int res = ActivityManagerNative.getDefault().bindService(
1783                mMainThread.getApplicationThread(), getActivityToken(),
1784                service, service.resolveTypeIfNeeded(getContentResolver()),
1785                sd, flags, user.getIdentifier());
1786            if (res < 0) {
1787                throw new SecurityException(
1788                        "Not allowed to bind to service " + service);
1789            }
1790            return res != 0;
1791        } catch (RemoteException e) {
1792            return false;
        }
1794    }


  注意在
1773对传入的intent进行了验证,继续追到validateServiceIntent`方法中:

private void validateServiceIntent(Intent service) {
    if (service.getComponent() == null && service.getPackage() == null) {
        if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
            IllegalArgumentException ex = new IllegalArgumentException(
                    "Service Intent must be explicit: " + service);
            throw ex;
        } else {
            Log.w(TAG, "Implicit intents with startService are not safe: " + service+ " " + Debug.getCallers(2, 3));
        }
    }
}

  这个方法是这篇文章的中心,逻辑也很清楚。当传入的intent满足getComponentgetPackage均为空的时候,如果当前应用的targetSdkVersion大于等于21,直接抛出异常Service Intent must be explicit,反之,只会给一个提示Implicit intents with startService are not safe。看到这里就争相大白了。顺便再看一下5.0以下是怎么处理的。
  这里是4.4.4的源码:

private void validateServiceIntent(Intent service) {
    if (service.getComponent() == null && service.getPackage() == null) {
        if (true || getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.KITKAT) {
            Log.w(TAG, "Implicit intents with startService are not safe: " + service+ " " + Debug.getCallers(2, 3));
            //IllegalArgumentException ex = new IllegalArgumentException(
            //        "Service Intent must be explicit: " + service);
            //Log.e(TAG, "This will become an error", ex);
            //throw ex;
        }
    }
}

`
  显然,这里也做了类似的判断,但是仅仅只会提示用户这样是不安全的而已。你也可以看到,抛异常的代码都已经写好了,只是注释掉了。至此就完全了解了这个bug产生的原因。

有任何疑问,欢迎加群讨论:261386924

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值