`
renyuan_1991
  • 浏览: 69499 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多
转载请注明出处:http://renyuan-1991.iteye.com/blog/2404247
今天记录并总结一下外部唤起app并传递参数相关的知识。
开门见山直接贴代码吧。
<activity
    android:name=".view.activity.UserLoginActivity"
    android:configChanges="keyboardHidden|orientation"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden|adjustPan">
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <data
             android:host="mytest.com"
             android:pathPrefix="/app"
             android:scheme="http">
        </data>
    </intent-filter>
</activity>

通过以上的设置,在包含“http://mytest.com/app”内容的短信或者网页中都可以打开我们的app。就这么简单!传入参数也很简单,如:“http://mytest.com/app?id = 100”便可向app传递参数,在activity中添加以下代码用于接收参数。

Intent intent = getIntent();
        String action = intent.getAction();
        if(Intent.ACTION_VIEW.equals(action)){
            Uri uri = intent.getData();
            if(uri != null){
                String id = uri.getQueryParameter("id");
                Log.i("MyLog-UserLoginActivity", "onCreate: id = " + id);
            }
        }


可是为什么我没打开呢?
第一:检查<intent-filter>是否是单独配置,如果需要打开的页面已经有了<intent-filter>,那最好再建一个<intent-filter>,不要混在一起,否者无效。
第二:请按照以上的格式编写,并检查设置的scheme和调用链接的scheme是否一致。action和category也要和例子中的一致,为什么要这样设置,他们代表了什么意思?别着急,后面会说。
第三:如果在网页中调用,请不要用自己写的webview。需要用系统的浏览器。
第四:以上三点都符合,为什么在短信中还是调不起来?那是因为有些手机的短信(主要跟手机的系统版本有关)只识别http合https开头的scheme。对于这一点目前没有找到解决办法,如果哪位大佬解决了这个问题请在留言中告诉我,定感激不尽!

<action android:name="android.intent.action.VIEW"></action>
的类型是有调用者决定的,我们想让系统的短息和浏览器打开我们的app,那就必须这么写,因为人家调用时指定的aciting是ACTION_VIEW。
<category android:name="android.intent.category.DEFAULT"></category>
是隐式意图调用时必须的配置
<category android:name="android.intent.category.BROWSABLE"></category>
是指定浏览器在特定情况下可以打开app。

就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下:
scheme://host:port/path
举个实际的例子:
content://com.example.project:200/folder/subfolder/etc
\---------/  \---------------------------/ \---/ \--------------------------/
scheme                 host               port        path
                \--------------------------------/
                          authority   

也就是说可以在data标签中添加一下这些信息
<data android:host="string"
      android:mimeType="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:port="string"
      android:scheme="string" />
转载请注明出处:[url] http://renyuan-1991.iteye.com/blog/2404247[/url]
希望爱好编程的小伙伴能加这个群,互相帮助,共同学习。群号: 141877583
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics