现在的位置: 首页 > 综合 > 正文

onNewIntent

2018年04月22日 ⁄ 综合 ⁄ 共 1747字 ⁄ 字号 评论关闭

一、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());

  1. public void onNewIntent(Intent intent) {  
  2.         final String action = intent.getAction();  
  3.         if (Intent.ACTION_SEARCH.equals(action)) {  
  4.             // Start query for incoming search request  
  5.             String query = intent.getStringExtra(SearchManager.QUERY);  
  6.             startNavigating(query, true);  
  7.   
  8.         } else if (Intent.ACTION_VIEW.equals(action)) {  
  9.             // 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(nulltrue);  
  19.         }  
  20.     }  

 

二、URI数据匹配

一个 Intent 可以通过 URI 携带外部数据给目标组件。在 <intent-filter >节点中,通过 <data/>节点匹配外部数据。

mimeType 属性指定携带外部数据的数据类型,scheme 指定协议,host、port、path 指定数据的位置、端口、和路径。如下:

  1. <data android:mimeType="mimeType" android:scheme="scheme"  
  2. 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相匹配。

抱歉!评论已关闭.