Android开发:显式/隐式Intent

时间:2024-04-26 06:08:41

显式跳转

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

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

我们也常这么用:

  1. Intent intent = new Intent(mContext, XXActivity.class);
  2. startActivity(intent);

这是跳转到当前应用的某个Activity,相信大家都十分熟悉,今天主要讲的是如何使用隐式intent意图跳转

隐式跳转

1、隐式跳转之Action跳转

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

那么我们就可以使用以下代码进行跳转到上面这个Activity中:

  1. //创建一个隐式的 Intent 对象:Action 动作
  2. Intent intent = new Intent();
  3. //设置 Intent 的动作为清单中指定的action
  4. intent.setAction("customer_action_here");
  5. startActivity(intent);

2、隐式跳转之Category跳转

假定有一个Activity在清单中是这样声明的:
  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:

  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是这样定义的:
  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>

我们可以使用如下代码进行跳转:

  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 使用浏览器浏览网页

  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 调用地图

  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 调用电话拨号(不需要拨号权限)

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

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

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

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

  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 调用短信程序(无需发送短信权限)

  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 调用邮件程序

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

4.8 调用音乐播放器

  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 调用视频播放器

  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 调用搜索

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

总结

相信大家经过上面的介绍,已经对Intent跳转有了一个比较成熟的理解,Intent是组件之间的纽带,使用它可以让系统替我们完成很多工作,不需要我们来指定完成工作的程序。实际上,我们会发现,调用系统程序使用液无非是隐式跳转,只不过使用的是系统内置的一些Action,Uri,Data等信息而已。希望对大家有所帮助。