您的位置:首页 > 移动开发 > Android开发

Android开发:显式/隐式Intent意图跳转Activity总结

2015-04-16 10:37 501 查看

显式跳转

是在已知包名和类名的情况下常用的跳转方法:

Intent mIntent = new Intent();
mIntent.setClassName("com.android.settings","com.android.settings.Settings");
mContext.startActivity(mIntent);


我们也常这么用:

Intent intent = new Intent(mContext, XXActivity.class);
startActivity(intent);
这是跳转到当前应用的某个Activity,相信大家都十分熟悉,今天主要讲的是如何使用隐式intent意图跳转

隐式跳转

1、隐式跳转之Action跳转

假定有一个Activity在清单中是这样的声明的:
<activity android:name=".ActionActivity"; 
        <intent-filter
            action android:name="customer_action_here"
        </intent-filter>
    </activity>


那么我们就可以使用以下代码进行跳转到上面这个Activity中:
//创建一个隐式的 Intent 对象:Action 动作
    Intent intent = new Intent();
    //设置 Intent 的动作为清单中指定的action
    intent.setAction("customer_action_here");
    startActivity(intent);


2、隐式跳转之Category跳转

假定有一个Activity在清单中是这样声明的:
<activity android:name=".CategoryActivity" >
            <intent-filter>
                <action android:name="customer_action_here" />
                <category android:name="customer_category_here" />
            </intent-filter>
        </activity>


我们可以使用如下代码进行跳转到以上Activity:
//创建一个隐式的 Intent 对象:Category 类别
 Intent intent = new Intent();
 intent.setAction("customer_action_here");
 //添加与清单中相同的自定义category
 intent.addCategory("customer_category_here");
 startActivity(intent);


3、隐式跳转之Data跳转

假定有一个Activity是这样定义的:
< activity android:name=".DataActivity">
     < intent-filter>
         < category android:name="android.intent.category.DEFAULT" />
         < data
             android:scheme="content"
             android:host="com.example.intentdemo"
             android:port="8080"
             android:pathPattern=".*pdf"
             android:mimeType="text/plain"/>
     < /intent-filter>
 < /activity>


我们可以使用如下代码进行跳转:
//创建一个隐式的 Intent 对象,方法四:Date 数据
 Intent intent = new Intent();
 Uri uri = Uri.parse("content://com.example.intentdemo:8080/abc.pdf");
 //注:setData、setDataAndType、setType 这三种方法只能单独使用,不可共用               
 //单独以 setData 方法设置 URI
 //intent.setData(uri);
 //单独以 seType 方法设置 Type
 //intent.setType("text/plain");
 //上面分步骤设置是错误的,要么以 setDataAndType 方法设置 URI 及 mime type
 intent.setDataAndType(uri, "text/plain");
 startActivity(intent);


清单中的port及以下属性时可选的,没有必要一定添加,但是添加了port及以下属性的话,java代码中的Uri中要做相应的匹配。

4、隐式跳转之调用系统应用

4.1 使用浏览器浏览网页

//web浏览器
 Uri uri= Uri.parse("http://www.baidu.com");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

4.2 调用地图

//打开地图查看经纬度
 Uri uri = Uri.parse("geo:38.899533,-77.036476");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);


4.3 调用电话拨号(不需要拨号权限)

Uri uri = Uri.parse("tel:10086");
    Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意区别于下面4.4的action
    startActivity(intent);

4.4 调用电话直接拨号(需要拨号权限)

Uri uri = Uri.parse("tel:15980665805");
    Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意区别于上面4.3的aciton
    startActivity(intent);


4.5 调用短信程序(无需发送短信权限,接收者自填)

Intent intent = new Intent(Intent.ACTION_VIEW);    
    intent.putExtra("sms_body", "这里写短信内容");    
    intent.setType("vnd.android-dir/mms-sms");    
    startActivity(intent);

4.6 调用短信程序(无需发送短信权限)

Uri uri = Uri.parse("smsto:10086");//指定接收者
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.putExtra("sms_body", "你这个黑心运营商");
    startActivity(intent);

4.7 调用邮件程序

Intent intent = new Intent(Intent.ACTION_SENDTO); 
    intent.setData(Uri.parse("mailto:xxx@gmail.com")); 
    intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题"); 
    intent.putExtra(Intent.EXTRA_TEXT, "这是内容"); 
    startActivity(intent);


4.8 调用音乐播放器

Intent intent = new Intent(Intent.ACTION_VIEW);
    //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");
    Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");
    intent.setDataAndType(uri, "audio/mp3");
    startActivity(intent);


4.9 调用视频播放器

Intent intent = new Intent(Intent.ACTION_VIEW);
    //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");
    Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");
    intent.setDataAndType(uri, "video/mp4");
    startActivity(intent);


调用视频播放器和音乐播放器的区别在setDataAndType()时一个是audio类型,一个是video类型,很容易记住,不允许使用其他意思相近的单词代替,代替无效。

4.10 调用搜索

Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_WEB_SEARCH); 
    intent.putExtra(SearchManager.QUERY, "android"); 
    startActivity(intent);


总结

相信大家经过上面的介绍,已经对Intent跳转有了一个比较成熟的理解,Intent是组件之间的纽带,使用它可以让系统替我们完成很多工作,不需要我们来指定完成工作的程序。实际上,我们会发现,调用系统程序使用液无非是隐式跳转,只不过使用的是系统内置的一些Action,Uri,Data等信息而已。希望对大家有所帮助。
转载请注明出处和链接:/article/2590628.html
这篇文章是的《Android开发:如何隐藏自己的app应用》续篇,感兴趣的朋友可以查看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐