【转】intent传递数据

 一、Intent作用

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

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

(1) 要激活一个新的Activity,或者让一个现有的Activity执行新的操作,可以通过调用Context.startActivity()或者Activity.startActivityForResult()方法。这两个方法需要传入的Intent参数也称为Activity Action Intent(活动行为意图),根据Intent对象对目标Activity描述的不同,来启动与之相匹配的Activity或传递信息。

(2) 要启动一个新的服务,或者向一个已有的服务传递新的指令,调用Context.startService()方法或调用Context.bindService()方法将调用此方法的上下文对象与Service绑定。

(3) 通过Context.sendBroadcast()、Context.sendOrderBroadcast()和Context.send-StickBroadcast()这三个方法可以发送BroadcastIntent。BroadcastIntent发送后,所有已注册的拥有与之相匹配IntentFilter的BroadcastReceiver就会被激活。这种机制被广泛运用于设备或系统状态变化的通知,一个常见的例子是,当Android的电池电量过低时,系统会发送Action为BATTERY_LOW的广播,接着任何可匹配该Action的IntentFilter注册的BroadcastReceiver都会各自运行自定义的处理代码,比如关闭设备的WIFI和GPS以节省电池消耗。

Intent一旦发出,Android都会准确找到相匹配的一个或多个Activity、Service或Broadcast-Receiver作为响应。所以,不同类型的Intent消息不会出现重叠:BroadcastIntent消息只会发送给BroadcastReceiver,而绝不可能发送给Activity或Service。由startActivity()传递的消息也只可能发送给Activity,由startService()传递的Intent只可能发送给Service。

二、使用Intent在多个Activity之间传递数据

 我们先重点讲解如何使用Intent在多个Activity之间传递数据,根据前面的介绍我们应该已经清楚,要从Activity1传递数据到Activity2重点是startActivity()和startActivityForResult()两个方法。

  1.无参数Activity跳转

  Intent it = new Intent(Activity1.this, Activity2.class);
  startActivity(it); 

方法1 实现从Activity1直接切换到Activity2.其中  Activity1和Activity2为窗口1和窗口2的类名,注意:这两个Activity一定要在AndroidManifest.xml中注册了才能打开。

 

2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)

     Intent it = new Intent(Activity1.this, Activity2.class);
    Bundle bundle=new Bundle();
    bundle.putString("name", "This is from MainActivity!");
    it.putExtras("bd",bundle);       // it.putExtra(“test”, "shuju”);
    startActivity(it);            // startActivityForResult(it,REQUEST_CODE);

方法2和方法1类似,但是实现了数据传递,注意Bundle对象类似于HashTable,可以存放一个键和对应的对象。而Intent对象也可以以键值对的方式存放bundle对象,从而实现在Activity1和

Acitivty2之间传递数据。在Activity2中可以通过以下方法获得Activity1中传递过来的数据

Intent intent = getIntent();
 Bundle bd = intent.getBundleExtra("bd");// 根据bundle的key得到对应的对象

 String name=bd.getString("name");

3.在Activity2中向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的Activity)

      Intent intent=getIntent();      
      Bundle bundle2=new Bundle();       
      bundle2.putString("name", "This is from ShowMsg!");      
      intent.putExtras(bundle2);       
      setResult(RESULT_OK, intent);

4.Activity1中如果要根据Activity2中传入的参数进行处理,必须在Activity1中的onActivityResult进行处理
@Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {      
           // TODO Auto-generated method stub     
     super.onActivityResult(requestCode, resultCode, data);   
     if (requestCode==REQUEST_CODE){         
             if(resultCode==RESULT_CANCELED)       
                  setTitle("cancle");          
             else if (resultCode==RESULT_OK) {           
                      String temp=null;              
                       Bundle bundle=data.getExtras();            
      if(bundle!=null)   temp=bundle.getString("name");      
           setTitle(temp);     
       }    
    }  
}

三、隐式Intent和运行时绑定 

隐式Intent是一种让匿名应用程序组件服务动作请求的机制。当创建一个新的隐式Intent时,你指定要执行的动作,作为可选项,你可以提供这个动作所需的数据。 

当你使用这个新的隐式Intent来启动Activity时,Android会在运行时解析它,找到最适合在指定的数据类型上执行动作的类。这意味着,你可以创建使用其它应用程序的工程,而不需要提前精确地知道你会借用哪个应用程序的功能。 

例如,如果你想让用户在应用程序里打电话,与其实现一个新的拨号,不如使用一个隐式的Intent来请求一个在一个电话号码(URI表示)上的动作(拨一个号码),如下代码片段所示: 

if (somethingWeird && itDontLookGood)

{

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:555-2368”));

startActivity(intent);

Android解析这个Intent并启动一个提供了能在一个号码上执行拨号动作的Activity,在这里,是拨号Activity。

 

下面是转载来的其他的一些Intent用法实例(转自javaeye)
显示网页
 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);   
 //其他 geo URI 範例  
 //geo:latitude,longitude  
 //geo:latitude,longitude?z=zoom  
 //geo:0,0?q=my+street+address  
 //geo:0,0?q=business+near+city  
 //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
路径规划
 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);  
 //用這個,要在 AndroidManifest.xml 中,加上  
 //<uses-permission id="android.permission.CALL_PHONE" /> 
传送SMS/MMS
 //调用短信程序 
 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); 

posted @ 2011-12-14 16:37  狼迹天涯  阅读(1047)  评论(0编辑  收藏  举报