Activity启动另一个Activity的方式总结

下面两种方式均采用这个menifests.xml布局,且下面两种方式属于显性方式{即通过使用(MainActivity.this, xxxx.class)};


一,用intent构造器进行启动:



二,用Component属性:


下面的方法是隐性方法即没有明确的指明(MainActivity.this, xxxx.class);

三,用Action和Category属性:

采用这个布局:


采用的Java:

使用action+category属性时,需要在代码中设置setAction和addCategory两个函数,但是如果布局中使用了属性为

<category android:name="android.intent.category.DEFAULT" />
的话可以只使用setAction,因为系统默认使用addCategory为以上属性的设置;

如上代码只使用了

intent.setAction(MainActivity.SNOW_ACTION);
没有设置addCategory();

需要注意的是如果不是采用系统的category的话,需要自己设置添加这样的布局和代码

<category android:name="snow_wind.test.intent.category.SNOW_CATEGORY" />
并在代码中设置

public final static String SNOW_CATEGORY ="snow_wind.test.intent.action.SNOW_CATEGORY";
intent.addCategory(SNOW_CATEGORY);
从上面的分析可以总结如下:

category和action其实就是两个字符串值,只要在intent中指明了这两个属性,Activity的manifests注册布局中如果包含了这两个属性,这个Activity就会被启动;

如果<activity......>包含了多个<action xxxx/>和多个<category xxxxxxx/>的话,且intent的设置属性包含<activity.....>中任意一组action和category的话,Activity就会响应;

这样的Activity可以响应多个intent;

四,用Action和Type属性

通过setAction和setType也可以启动一个Activity

这里的代码取之一个用与启动系统Action所对应的Activity(系统给出很多action可通过API GUIDES中的intent 

and intent filter项来查找)因为启动的是系统Activity所以manifests文件中不会注册<activity......>和<action xxx/>的;

// 设置IntentAction属性
intent.setAction(Intent.ACTION_GET_CONTENT);
// 设置IntentType属性
intent.setType("vnd.android.cursor.item/phone");
五,用Data属性启动Activity或用Data+Type属性启动Activity

使用data属性启动Activity需要在manifests中加入

<intent-filter>
    <action android:name="xx"/>
    <category android:name="android.intent.category.DEFAULT" />
    <!-- 需要IntentData属性的schemelee,且hostwww.fkjava.org
    port8888,且path/mypath
    typeabc/xyz,才可启动该Activity -->
    <data android:scheme="lee"
        android:host="www.fkjava.org"
        android:port="8888"
        android:path="/mypath"
        android:mimeType="abc/xyz"/>
</intent-filter>
data的属性如上所示,其中mimeType属于Type的属性,二者均放在<data...../>中进行定义;

在代码中只要加入

   Intent intent = new Intent();
   // 同时设置IntentDataType属性
   intent.setDataAndType(Uri.parse("lee://www.fkjava.org:8888/mypath")
      , "abc/xyz");
   startActivity(intent);

即可;(这里使用data+type属性);

如果仅使用data属性的话可以这样:

A:

<data android:scheme="lee" />

intent.setData(Uri.parse("lee://www.crazyit.org:1234/test"));

B:

<data android:scheme="lee"
    android:host="www.fkjava.org"
    android:port="8888" />
intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));
c:
<data android:scheme="lee"
    android:host="www.fkjava.org"
    android:path="/mypath" />

intent.setData(Uri.parse("lee://www.fkjava.org:1234/mypath"));
D:

<data android:scheme="lee"
    android:host="www.fkjava.org"
    android:port="8888"
    android:path="/mypath"/>
intent.setData(Uri.parse("lee://www.fkjava.org:8888/mypath"));
E:
<data android:scheme="lee"
    android:host="www.fkjava.org"
   />

intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));liiu

六,使用Action+Data属性

在manifests中不需要写入任何属性,只是传入数据data和使用Action调用系统Activity;(调用系统编辑Activity)

Intent intent = new Intent();
// Intent设置Action属性(动作为:编辑)
intent.setAction(Intent.ACTION_EDIT);
String data = "content://com.android.contacts/contacts/1";
// 根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
// 设置Data属性
intent.setData(uri);
startActivity(intent);
调用打开网站的默认浏览器启动的Activity;

Intent intent = new Intent();
String data = "http://www.crazyit.org";
// 根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
// Intent设置Action属性
intent.setAction(Intent.ACTION_VIEW);
// 设置Data属性
intent.setData(uri);
startActivity(intent);
调用系统拨号应用,并给出电话号码;

Intent intent = new Intent();
// Intent设置Action属性(动作为:拨号)
intent.setAction(Intent.ACTION_DIAL);
String data = "tel:13800138000";
// 根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
// 设置Data属性
intent.setData(uri);
startActivity(intent);
以上启动的都是系统的应用程序,用它们来完成data所传入的数据;




  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值