Intent学习

来源:互联网 发布:python 多核并行计算 编辑:程序博客网 时间:2024/06/06 03:33

不管是页面调转,还是传递数据,或是调用外部程序,系统功能都要用到intent。

Android的有三个基本组件——Activity,Service和BroadcastReceiver,它们都是通过Intent机制激活的,而不同类型的组件有传递Intent的不同方式。

 

显示调用和隐式调用的区别:

显示一般用于调用自己程序中的activity。隐式一般用于调用其他程序中的activity,或自己的程序要向外提供接口,使其他程序可以调用自己程序的activity。显式只能

调用唯一的activity,而隐式可能调用一个或多个activity,如果是多个activity,会弹出一个菜单由用户选择。

      显示intent,这种方式通过指定Intent组件名称来实现。 
       隐式intent,这种方式通过IntentFilter实现。 

 

Intent Filter进行过滤时通常考虑三个属性: Action(动作),Categroy(分类),Data(数据)

Action:如Intent指明定了Action,则目标组件IntentFilter的Action列表中就必须包含这个Action,否则不能匹配。 
       Category:在intent对象中出现的Category属性在IntentFilter中必须出现,否则不能通过测试。 
   ”android.intent.category.DEFAULT“属性是启动Activity默认属性,这个必须添加,否则Category测试失败。 
        Data:Data是Android要访问的数据和Action和Category声明方式一致。 
      在AndroidMainifest.xml中 
       <intent——filter> 
         <action android:name="android.intent.action.VIEW"/> 
         <action android:scheme="http" android:path=""www.g.cn/> 
       <intet-filter> 
      MainActivity中Intent对象设置Action和Data属性同上 
        intent.setAction("android.intent.action.VIEW"); 
       intent.setData()Uri.parse("http://www.g.cn"); 

 

 Action是指Intent要完成的动作,是一个字符串常量。 
  如ACTION_CALL(打电话),ACTION_EDIT(编辑数据) 

 1.自定义Action属性 
     MainACtivity: 
     public static final String MYACTION="com.a=maker.app.MY_ACTION"; 
    Intent intent=new Intent(); 
    intent.setACTION(MY_ACTION); 
    startActivity(intent); 
     MyACtivity: 
     Intent intent=getIntent(); 
     String action=intent.getAction(); 
     AndroidMainifest.xml 
    <activity android:name="MyActivity"> 
    <intent-filter> 
     <action android:name="com.amaker.ch06.app.MY_ACTION"> 
     <categroy anndroid:name="android.intent.category.DEFAULT"> 
    </activity> 
   2.访问系统Action属性 
      Intent intent=new Intent(); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     intent.setType("Vnd.android.cursor.item/phone"); 
     startActivity(intent); 

 Intent的data属性是执行动作的URI和MIME类型,不同的Action有不同的Data数据  指定。 

Intent中的category属性石一个执行Action的附加信息。 
    例:CATEGORY_LAUNCHER意味着加载程序时,Activity出现在最上面,还有CATEGORY_HOME,则表示回到HOME页面。 

 

intent隐式调用:

显示网页
 Uri uri = Uri.parse("http://google.com/");  
 Intent it = new Intent(Intent.ACTION_VIEW, uri);  
 startActivity(it);
显示地图
 Uri uri = Uri.parse("geo:38.899533,-77.036476");  
 Intent it = new Intent(Intent.ACTION_VIEW, uri);   
 startActivity(it); 

路径规划
 Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");  
 Intent it = new Intent(Intent.ACTION_VIEW, uri);  
 startActivity(it);  
 //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 
打电话
 //叫出拨号程序 
 Uri uri = Uri.parse("tel:0800000123");  
 Intent it = new Intent(Intent.ACTION_DIAL, uri);  
 startActivity(it);  
 //直接打电话出去  
 Uri uri = Uri.parse("tel:0800000123");  
 Intent it = new Intent(Intent.ACTION_CALL, uri);  
 startActivity(it);  

//调用短信程序 
 Intent it = new Intent(Intent.ACTION_VIEW, uri);  
 it.putExtra("sms_body", "The SMS text");   
 it.setType("vnd.android-dir/mms-sms");  
 startActivity(it); 
 //传送消息 
 Uri uri = Uri.parse("smsto://0800000123");  
 Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
 it.putExtra("sms_body", "The SMS text");  
 startActivity(it); 
 //传送 MMS  
 Uri uri = Uri.parse("content://media/external/images/media/23");  
 Intent it = new Intent(Intent.ACTION_SEND);   
 it.putExtra("sms_body", "some text");   
 it.putExtra(Intent.EXTRA_STREAM, uri);  
 it.setType("image/png");   
 startActivity(it); 
传送 Email
 Uri uri = Uri.parse("mailto:xxx@abc.com");  
 Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
 startActivity(it);

 Intent it = new Intent(Intent.ACTION_SEND);  
 it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");  
 it.putExtra(Intent.EXTRA_TEXT, "The email body text");  
 it.setType("text/plain");  
 startActivity(Intent.createChooser(it, "Choose Email Client"));

 Intent it=new Intent(Intent.ACTION_SEND);    
 String[] tos={"me@abc.com"};    
 String[] ccs={"you@abc.com"};    
 it.putExtra(Intent.EXTRA_EMAIL, tos);    
 it.putExtra(Intent.EXTRA_CC, ccs);    
 it.putExtra(Intent.EXTRA_TEXT, "The email body text");    
 it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");    
 it.setType("message/rfc822");    
 startActivity(Intent.createChooser(it, "Choose Email Client"));

 //传送附件
 Intent it = new Intent(Intent.ACTION_SEND);  
 it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
 it.putExtra(Intent.EXTRA_STREAM, "http://www.cnblogs.com/l_dragon/admin/file:///sdcard/mysong.mp3");  
 sendIntent.setType("audio/mp3");  
 startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体
       Uri uri = Uri.parse("http://www.cnblogs.com/l_dragon/admin/file:///sdcard/song.mp3");  
       Intent it = new Intent(Intent.ACTION_VIEW, uri);  
       it.setType("audio/mp3");  
       startActivity(it); 
       Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");  
       Intent it = new Intent(Intent.ACTION_VIEW, uri);  
       startActivity(it);
Market 相关
        //寻找某个应用 
        Uri uri = Uri.parse("market://search?q=pname:pkg_name"); 
        Intent it = new Intent(Intent.ACTION_VIEW, uri);  
        startActivity(it);  
        //where pkg_name is the full package path for an application 
        //显示某个应用的相关信息 
        Uri uri = Uri.parse("market://details?id=app_id");  
        Intent it = new Intent(Intent.ACTION_VIEW, uri); 
        startActivity(it);  
        //where app_id is the application ID, find the ID   
        //by clicking on your application on Market home   
        //page, and notice the ID from the address bar
Uninstall 应用程序
        Uri uri = Uri.fromParts("package", strPackageName, null); 
        Intent it = new Intent(Intent.ACTION_DELETE, uri);   
        startActivity(it); 

 

 

PendingIntent和Intent的区别:

 以前在学习AlarmManager里面会遇到PendingIntent,相信大家都知道Intent是你的意图,比如你想启动一个Activity,就会通过 Intent来描述启动这个Activity的某些特点,让系统找到这个Activity来启动,而不是启动别的 Activity.StartActivity(intent)就会立即启动这个Activity.而PendingIntent呢?Penging中文意思就是:待定,将来发生或来临。PendingIntent的就的意思就是不是像Intent那样立即发生,而是在合适的时候才会去触发对应的 Intent.有人说这个intent不是你的ap来触发而是交给别的ap来触发。

Intent 表示一个目的,第一个参数表示所在类,第二个参数表示目标类
PendingIntent 即是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情
换种说法Intent 字面意思是意图,即我们的目的,我们想要做的事情,在activity中,我们可以立即执行它
PendingIntent 相当于对intent执行了包装,我们不一定一定要马上执行它,我们将其包装后,传递给其他activity或application
这时,获取到PendingIntent  的application 能够根据里面的intent 来得知发出者的意图,选择拦击或者继续传递或者执行.