intent应用实例

 

1,显示匹配(Explicit): html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public  TestB extents Activity
{
 .........
};
 public  class  Testextends  Activity
{
     ......
     public  void  switchActivity()
     {
            Intent i =new  Intent(Test.this, TestB.class);
            this.startActivity(i);
     }
}

 

 

2,隐式匹配(Implicit):
   
  
隐式匹配,首先要匹配Intent的几项值:Action, Category, Data/Type,Component。若是填写了Componet就是上例中的Test.class)这就造成了显示匹配。因此此部分只讲前几种匹配。匹配规则为最大匹配规则,

1,若是你填写了Action,若是有一个程序的Manifest.xml中的某一个Activity的IntentFilter段中定义了包含了相同的Action那么这个Intent就与这个目标Action匹配,若是这个Filter段中没有定义Type,Category,那么这个Activity就匹配了。可是若是手机中有两个以上的程序匹配,那么就会弹出一个对话可框来提示说明。
Action的值在Android中有不少预约义,若是你想直接转到你本身定义的Intent接收者,你能够在接收者的IntentFilter中加入一个自定义的Action值(同时要设定Category值为"android.intent.category.DEFAULT"),在你的Intent中设定该值为Intent的Action,就直接能跳转到你本身的Intent接收者中。由于这个Action在系统中是惟一的。 java


2,data/type,你能够用Uri来作为data,好比Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手机的Intent分发过程当中,会根据http://www.google.com 的scheme判断出数据类型type
手机的Brower则能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能处理http:的type,

3,至于分类Category,通常不要去在Intent中设置它,若是你写Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,这样全部不设置Category(Intent.addCategory(String c);)的Intent都会与这个Category匹配。

4,extras(附加信息),是其它全部附加信息的集合。使用extras能够为组件提供扩展信息,好比,若是要执行“发送电子邮件”这个动做,能够将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。 android

 

 

 

 

用 Intent 激活电话拨号程序 web

 

  这里使用一个Intent打开电话拨号程序,Intent的行为是ACTION_DIAL,同时在Intent中传递被呼叫人的电话号码。 浏览器

  在用户界面中加入一个Button按钮,编辑res/layout/main.xml文件: app

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0"  encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id ="@+id/button_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
/>
</LinearLayout><span style="font-family: verdana, 'courier new'; font-size: 14pt;"><span style="white-space: normal;">
</span></span>

 

咱们把Button的id设置为button_id, 同时将Button显示在界面上的文字设置为res/string.xml/下的Button,打开res/string.xml,把button的内容设置为“拨号”: ide

 

?
1
2
3
4
5
6
<?xml version="1.0"  encoding="utf-8"?>
<resources>
<string name="button">拨号</string>
<string name="app_name">TinyDialer</string>
</resources><span style="font-family: verdana, 'courier new'; font-size: 14pt;"><span style="white-space: normal;">
</span></span>

建立TinyDialer的Activity ui

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  class  TinyDialerextends  Activity {
 /** Called when the Activity is first created. */
 @Override
 public  void  onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    final  Button button = (Button) findViewById(R.id.button_id);
    button.setOnClickListener(new  Button.OnClickListener() {
      @Override
      public  void  onClick(View b) {
         Intent i =new  Intent(Intent.ACTION_DIAL,
                      Uri.parse(" tel://13800138000"));
         startActivity(i);
      }
    });
 }
}

 

 

 

 

 

 

用Intent调用系统中常常被用到的组件 this

 

 

1,web浏览器 google

Uri uri= Uri.parse("http://kuikui.javaeye.com");

returnIt = new Intent(Intent.ACTION_VIEW, uri);

2,地图

Uri mapUri = Uri.parse("geo:38.899533,-77.036476");

returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

3,调拨打电话界面

Uri telUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_DIAL, telUri);

4,直接拨打电话

Uri callUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_CALL, callUri);

5,卸载

Uri uninstallUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

6,安装

Uri installUri = Uri.fromParts("package", "xxx", null);

returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

returnIt = new Intent(Intent.ACTION_VIEW, playUri);

8,掉用发邮件

Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");

returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

9,发邮件

returnIt = new Intent(Intent.ACTION_SEND);

String[] tos = { "shenrenkui@gmail.com" };

String[] ccs = { "shenrenkui@gmail.com" };

returnIt.putExtra(Intent.EXTRA_EMAIL, tos);

returnIt.putExtra(Intent.EXTRA_CC, ccs);

returnIt.putExtra(Intent.EXTRA_TEXT, "body");

returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");

returnIt.setType("message/rfc882");

Intent.createChooser(returnIt, "Choose Email Client");

10,发短信

Uri smsUri = Uri.parse("tel:100861");

returnIt = new Intent(Intent.ACTION_VIEW, smsUri);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.setType("vnd.android-dir/mms-sms");

11,直接发邮件

Uri smsToUri = Uri.parse("smsto://100861");

returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);

returnIt.putExtra("sms_body", "shenrenkui");

12,发彩信

Uri mmsUri = Uri.parse("content://media/external/images/media/23");

returnIt = new Intent(Intent.ACTION_SEND);

returnIt.putExtra("sms_body", "shenrenkui");

returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);

returnIt.setType("image/png");

相关文章
相关标签/搜索