intent 显示/隐式跳转actiivty

显式跳转

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

[java]  view plain  copy
  1. Intent mIntent = new Intent();  
  2. mIntent.setClassName("com.android.settings","com.android.settings.Settings");  
  3. mContext.startActivity(mIntent);  

我们也常这么用:

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

隐式跳转

1、隐式跳转之Action跳转

假定有一个Activity在清单中是这样的声明的:
[html]  view plain  copy
  1. <activity android:name=".ActionActivity";   
  2.      <intent-filter  
  3.          action android:name="customer_action_here"  
  4.      </intent-filter>  
  5.  </activity>  

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

2、隐式跳转之Category跳转

假定有一个Activity在清单中是这样声明的:
[html]  view plain  copy
  1. <activity android:name=".CategoryActivity" >  
  2.     <intent-filter>  
  3.         <action android:name="customer_action_here" />  
  4.         <category android:name="customer_category_here" />  
  5.     </intent-filter>  
  6. </activity>  

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

3、隐式跳转之Data跳转

假定有一个Activity是这样定义的:
[html]  view plain  copy
  1. < activity android:name=".DataActivity">  
  2.     < intent-filter>  
  3.         < category android:name="android.intent.category.DEFAULT" />  
  4.         < data  
  5.             android:scheme="content"  
  6.             android:host="com.example.intentdemo"  
  7.             android:port="8080"  
  8.             android:pathPattern=".*pdf"  
  9.             android:mimeType="text/plain"/>  
  10.     < /intent-filter>  
  11. < /activity>  

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

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

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

4.1 使用浏览器浏览网页

[java]  view plain  copy
  1. //web浏览器  
  2. Uri uri= Uri.parse("http://www.baidu.com");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(intent);  

4.2 调用地图

[java]  view plain  copy
  1. //打开地图查看经纬度  
  2. Uri uri = Uri.parse("geo:38.899533,-77.036476");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(intent);  

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

[java]  view plain  copy
  1. Uri uri = Uri.parse("tel:10086");  
  2. Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意区别于下面4.4的action  
  3. startActivity(intent);  

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

[java]  view plain  copy
  1. Uri uri = Uri.parse("tel:15980665805");  
  2. Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意区别于上面4.3的aciton  
  3. startActivity(intent);  

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

[java]  view plain  copy
  1. Intent intent = new Intent(Intent.ACTION_VIEW);      
  2. intent.putExtra("sms_body""这里写短信内容");      
  3. intent.setType("vnd.android-dir/mms-sms");      
  4. startActivity(intent);  

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

[java]  view plain  copy
  1. Uri uri = Uri.parse("smsto:10086");//指定接收者  
  2. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);  
  3. intent.putExtra("sms_body""你这个黑心运营商");  
  4. startActivity(intent);  

4.7 调用邮件程序

[java]  view plain  copy
  1. Intent intent = new Intent(Intent.ACTION_SENDTO);   
  2. intent.setData(Uri.parse("mailto:[email protected]"));   
  3. intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题");   
  4. intent.putExtra(Intent.EXTRA_TEXT, "这是内容");   
  5. startActivity(intent);  

4.8 调用音乐播放器

[java]  view plain  copy
  1. Intent intent = new Intent(Intent.ACTION_VIEW);  
  2. //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  
  3. Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  
  4. intent.setDataAndType(uri, "audio/mp3");  
  5. startActivity(intent);  

4.9 调用视频播放器

[java]  view plain  copy
  1. Intent intent = new Intent(Intent.ACTION_VIEW);  
  2. //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  
  3. Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  
  4. intent.setDataAndType(uri, "video/mp4");  
  5. startActivity(intent);  

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

4.10 调用搜索

[java]  view plain  copy
  1. Intent intent = new Intent();   
  2. intent.setAction(Intent.ACTION_WEB_SEARCH);   
  3. intent.putExtra(SearchManager.QUERY, "android");   
  4. startActivity(intent);  

猜你喜欢

转载自blog.csdn.net/sinat_22949049/article/details/80064261