您的位置:首页 > 其它

onNewIntent

2012-10-31 15:30 134 查看
一、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());

[java]
view plaincopy

public void onNewIntent(Intent intent) {  
        final String action = intent.getAction();  
        if (Intent.ACTION_SEARCH.equals(action)) {  
            // Start query for incoming search request  
            String query = intent.getStringExtra(SearchManager.QUERY);  
            startNavigating(query, true);  
  
        } else if (Intent.ACTION_VIEW.equals(action)) {  
            // Treat as internal link only if valid Uri and host matches  
            Uri data = intent.getData();  
            if (data != null && ExtendedWikiHelper.WIKI_LOOKUP_HOST  
                    .equals(data.getHost())) {  
                String query = data.getPathSegments().get(0);  
                startNavigating(query, true);  
            }  
        } else {  
            // If not recognized, then start showing random word  
            startNavigating(null, true);  
        }  
    }  

 

二、URI数据匹配

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

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

[java]
view plaincopy

<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相匹配。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: