Android开发过程踩坑记录-持续更新

2018-6-14 关于Intent

当我们隐式启动一个service时

Intent it = new Intent("xxx.RemoteService");
bindService(it, mRemoteConnectioin, BIND_AUTO_CREATE);

如果参数 action里边的 xxx是随意写的, 会报错.

Process: com.example.android.test01, PID: 27678
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cccc/com.ccc.aaaServiceActivity}:
java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { >act=xxxx.remoteservice }
at android.app.Activi

这时我一百度, 大部分都说是 要显示Intent: 如:
" 通过Action的隐式Intent启动Service在安卓5.0以前的版本是可以实现跨应用启动Service的,安卓5.0以后的版本若想实现
跨应用启动Service的功能必须使用显示Intent"。

这显示是不行的.

解决 ; 查看ap注释

/**
 * Create an intent with a given action.  All other fields (data, type,
 * class) are null.  Note that the action <em>must</em> be in a
 * namespace because Intents are used globally in the system -- for
 * example the system VIEW action is android.intent.action.VIEW; an
 * application's custom action would be something like
 * com.google.app.myapp.CUSTOM_ACTION.
 即上边的 action必须在一个 namespace里边, 因为intent被系统全局应用.
 *
 * @param action The Intent action, such as ACTION_VIEW.
 */
public Intent(String action) {
    setAction(action);
}

翻译注释得知:即上边的 action必须在一个 namespace里边, 因为intent被系统全局应用.
那么怎么才能在一个 namespace里边呢.显示 我们setPackage就可以了.增加代码

it.setPackage("com.aaa.bbb");

所以. 不知道原因时, 多看看注释是不错的哦.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,886评论 25 709
  • 注:本文完全拷贝自https://developer.android.com/guide/components/i...
    RxCode阅读 1,821评论 1 13
  • 文/抒同 (一) 五月一号那天我吃过晚饭在电脑桌前正襟危坐,思考小说《指印》该怎么继续往下写。就在前一天晚上我发表...
    抒同阅读 497评论 9 5
  • 今天我想告诉自己,既然选择了远方,便只顾风雨兼程,生活若继续过得如此枯燥苟且,为什么还要来白白走这一遭。 今天我最...
    blue布拉格girls阅读 512评论 0 0
  • 1-自我的力量,就是指一个人的人格中,蕴含着的力量。 自我的疆界,则是讲的一个人的自我,可以伸展开的空间。它和掌控...
    依诺芝阅读 284评论 0 0