国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

31.顯示隱式跳轉(zhuǎn)activity

2019-11-09 18:10:33
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

安卓中經(jīng)常涉及到activity的跳轉(zhuǎn),跳轉(zhuǎn)怎么樣在安卓中實(shí)現(xiàn)呢,如下。

創(chuàng)建第二個(gè)activity就是創(chuàng)建一個(gè)class,繼承自android.app.Activity.創(chuàng)建第二個(gè)activity的同時(shí)需要在清單文件中配置,不然會(huì)找不到

<activity android:name="com.ldw.createActivity.SecondActivity"></activity> 入口activity有下面的代碼,只要activity有下面的代碼,就會(huì)創(chuàng)建一個(gè)圖標(biāo)。默認(rèn)圖標(biāo)是一樣的

可以通過(guò)android:lable=“”來(lái)設(shè)置圖標(biāo)的名字。

<intent-filter>       <action android:name="android.intent.action.MAIN" />       <category android:name="android.intent.category.LAUNCHER" /></intent-filter>

如果activity所在的包跟應(yīng)用包名同名,則可以不寫(xiě)。

完整的清單中的配置如下:

        <activity            android:name="com.ldw.activityto.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>隱式跳轉(zhuǎn)和顯示跳轉(zhuǎn)

顯示跳轉(zhuǎn)到activity顯示跳轉(zhuǎn)中清單文件需要添加下面的配置

 <activity android:name="com.ldw.activityto.SecondActivity"></activity>

代碼中的實(shí)現(xiàn)如下

    /*     * 跳轉(zhuǎn)到本應(yīng)用中的activity     * 顯示跳轉(zhuǎn):直接指定目標(biāo)activity的包名和類名     */    public void click2(View v){    	Intent intent = new Intent();    	//第一個(gè)參數(shù)是上下文對(duì)象,第二個(gè)參數(shù)是制定目的activity的類名    	//顯示意圖    	intent.setClass(this, SecondActivity.class);    	startActivity(intent);    }

隱式跳轉(zhuǎn)到activityintent-filter意圖過(guò)濾器中有3個(gè)參數(shù)action,category,data。action和data可以配置多個(gè)。category是系統(tǒng)的配置,action中的name是自己隨便定義的,定義好以后name的值就是activity的動(dòng)作,隱式啟動(dòng)activity時(shí),意圖中的配置必須和這里的action的name是一致的。data是跳轉(zhuǎn)的過(guò)程中攜帶的參數(shù),mimeType是攜帶的數(shù)據(jù)的類型,根據(jù)意圖過(guò)濾器中中的配置,跳轉(zhuǎn)中針對(duì)data的配置需要做不同的處理。

        <activity android:name="com.ldw.activityto.SecondActivity">            <intent-filter>                <action android:name="com.ldw.activityto.sa"/>		    <action android:name="com.ldw.activityto.sasa"/>		    <data android:scheme="ldw" android:mimeType="text/passWord"/>                <category android:name="android.intent.category.DEFAULT"/>            </intent-filter>        </activity>

代碼中的實(shí)現(xiàn)如下

    /*     * 隱式跳轉(zhuǎn)到撥secondActivity     */    public void click5(View v){    	Intent intent = new Intent();    	//目標(biāo)activity的包名和類名    	intent.setAction("com.ldw.activityto.sa");    	intent.setData(Uri.parse("ldw:canshu")); //scheme中的參數(shù)加上冒號(hào),沒(méi)有miniType時(shí)候的配置    	//intent.setType("text/password");//沒(méi)有配置data卻有miniType的時(shí)候的配置    	//intent.setDataAndType(Uri.parse("ldw:canshu"), "text/password");//data和miniType都有的時(shí)候的    	intent.addCategory(Intent.CATEGORY_DEFAULT);//不寫(xiě)這句系統(tǒng)就添加默認(rèn)的category    	startActivity(intent);    }

activity中獲取到傳遞的參數(shù)的方法:

package com.ldw.activityto;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;public class SecondActivity extends Activity {	@Override	PRotected void onCreate(Bundle savedInstanceState){		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_second);				//獲取到啟動(dòng)這個(gè)activity的意圖		Intent intent = getIntent();		//獲取到傳遞過(guò)來(lái)的數(shù)據(jù)		Uri uri = intent.getData();	}}

如何選擇哪一種啟動(dòng)方式:啟動(dòng)同一個(gè)應(yīng)用中的activity適合用顯示,啟動(dòng)不同應(yīng)用中的activiy適合用隱式。全部使用隱式是完全沒(méi)有問(wèn)題的,使用顯示的效率更高一些。當(dāng)系統(tǒng)中有多個(gè)activity與意圖設(shè)置的Action匹配,那么啟動(dòng)Activity時(shí),會(huì)彈出對(duì)話框,里面包含匹配的Activity。

打電話應(yīng)用的配置 
   /*     * 跳轉(zhuǎn)到打電話activity     * 隱式跳轉(zhuǎn):通過(guò)制定action和data來(lái)跳轉(zhuǎn)     */    public void click1(View v){    	Intent intent = new Intent();    	//隱式意圖    	intent.setAction(Intent.ACTION_CALL);    	intent.setData(Uri.parse("tel:1190"));    	//跳轉(zhuǎn)    	startActivity(intent);    }    /*     * 顯示跳轉(zhuǎn)到撥號(hào)器     */    public void click3(View v){    	Intent intent = new Intent();    	//目標(biāo)activity的包名和類名    	intent.setClassName("com.android.dialer", ".DialtactsActivity");    	startActivity(intent);    }啟動(dòng)瀏覽器的方式   
 /*     * 顯示跳轉(zhuǎn)到瀏覽器     */    public void click6(View v){    	Intent intent = new Intent();    	//目標(biāo)activity的包名和類名    	intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");    	startActivity(intent);    }    /*     * 隱式跳轉(zhuǎn)到瀏覽器     */    public void click7(View v){    	Intent intent = new Intent();    	//目標(biāo)activity的包名和類名    	intent.setAction(intent.ACTION_VIEW);    	intent.setData(Uri.parse("http://www.baidu.com"));    	startActivity(intent);    }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁阳县| 宜兴市| 定陶县| 江永县| 康乐县| 神木县| 新巴尔虎左旗| 西乡县| 阿克苏市| 楚雄市| 汪清县| 通海县| 沭阳县| 溆浦县| 南充市| 阳高县| 万载县| 陆良县| 承德县| 南阳市| 宣恩县| 英山县| 库车县| 盐边县| 淅川县| 金溪县| 南投市| 蒲江县| 山东| 南城县| 惠水县| 镇宁| 长葛市| 乌苏市| 财经| 乳山市| 来宾市| 洪雅县| 新昌县| 芜湖县| 邢台县|