Android:onNewIntent的使用
一、onNewIntent(Intent intent)方法
该方法是activity的一个保护类型的方法,利用已有的Acivity去处理别的Intent时,你就可以利用onNewIntent来处理,通常被用在有搜索请求的activity,而其该activity有好几个intent-filter,该方法被调用的前提
a、该activity设置如下属性 android:launchMode="singleTop"。
b、该activity已经处在栈的顶端,通过其他的方法又重新启动该acitvity时被调用,如搜索,这时oncreate()方法不调用。
在这个例子中onNewIntent(Intent intent) 用来处理所有的Intent,在oncreate()方法中也认为调用了onNewIntent(getIntent());
01.public void onNewIntent(Intent intent) { 02. final String action = intent.getAction(); 03. if (Intent.ACTION_SEARCH.equals(action)) { 04. // Start query for incoming search request 05. String query = intent.getStringExtra(SearchManager.QUERY); 06. startNavigating(query, true); 07. 08. } else if (Intent.ACTION_VIEW.equals(action)) { 09. // Treat as internal link only if valid Uri and host matches 10. Uri data = intent.getData(); 11. if (data != null && ExtendedWikiHelper.WIKI_LOOKUP_HOST 12. .equals(data.getHost())) { 13. String query = data.getPathSegments().get(0); 14. startNavigating(query, true); 15. } 16. } else { 17. // If not recognized, then start showing random word 18. startNavigating(null, true); 19. } 20. }
二、URI数据匹配
一个 Intent 可以通过 URI 携带外部数据给目标组件。在 <intent-filter >节点中,通过 <data/>节点匹配外部数据。
mimeType 属性指定携带外部数据的数据类型,scheme 指定协议,host、port、path 指定数据的位置、端口、和路径。如下:
<data android:mimeType="mimeType" android:scheme="scheme" android:host="host" android:port="port" android:path="path"/>
如果在 Intent Filter 中指定了这些属性,那么只有所有的属性都匹配成功时 URI 数据匹配才会成功。
该例子中通过String.format("<a href=/"%s://%s/$1/">$1</a>", WIKI_AUTHORITY, WIKI_LOOKUP_HOST)));把链接格式化成<a href="wiktionary://lookup/ah">ah</a>形式的,这样就可以和配置文件中的data相匹配。
更多相关文章
- SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
- 一句话锁定MySQL数据占用元凶
- android中httpclient和HttpURLConnection优缺点和常见bug解决方
- 全局获取Context的技巧(再也不要为获取Context而感到烦恼)
- 梳理Android的IPC进程间通信(最新AndroidStudio的AIDL操作)
- Android(安卓)Adapter总结
- Android(安卓)Studio3.0 apk安装时提示受损解决方法
- Android(安卓)应用性能优化
- 初学android第一章、第二章