步步为营_Android开发课[4]_Intent学习

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主题:Intent学习
    -Intent(意图)主要是解决Android应用的各项组件之间的通讯问题。Intent消息传递是一种组件间运行时绑定的机制。

Intent和Bundle机制

Intent和Bundle实现从一个Activity带参数转换到另一个Activity的代码例子

在activity1的Intent里绑定信息。

    Intent intent = new Intent(active1.this, active2.class);
    Bundle bundle = new Bundle();
    bundle.putString("NAME", "蛋蛋");
    intent.putExtras(bundle);
    startActivity(intent);

再到activity2里取出信息。

    Bundle bundle = this.getIntent().getExtras();
    String str = bundle.getString("NAME");
    Log.v("tag",str);//在Logcat窗口打印获取的信息

Intent和 Intent Filter区别:

-Intent在Android中被翻译为”意图”,它是activity,service和broadcast receiver之间互相激活时传递信息的”信使”。它类似于我们WEB中的HttpServletRequest.
-而Intent Filter相当于我们给Servlet配置的url-patten, 用于匹配某个具体的Intent对象.

Intent用法举例:

显示网页:

Uri uri = Uri.parse("http://www.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");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);

拨打电话:

Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);  
startActivity(it);  
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);

要使用这个必须在配置文件中加入
<uses-permission id="android.permission.CALL_PHONE" />

发送短信

Uri uri = Uri.parse("smsto:0800000123");   
Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
it.putExtra("sms_body", "The SMS text");   
startActivity(it); 

发送彩信

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, "file:///sdcard/mysong.mp3");   
sendIntent.setType("audio/mp3");   
startActivity(Intent.createChooser(it, "Choose Email Client"));

播放多媒体

Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "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);  

Uninstall 程序

Uri uri = Uri.fromParts("package", strPackageName, null);   
Intent it = new Intent(Intent.ACTION_DELETE, uri);   
startActivity(it);

Intent中传递对象的两种方法

一种是Bundle.putSerializable(Key,Object);
另一种是Bundle.putParcelable(Key,Object);
当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口。

很多人知道在activity之间传递对象时有两个选择一个是Parcelable,另外一个是Serializable。但究竟我们该何时使用其中的一个呢?

1.在使用内存的时候,Parcelable类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点,也不提倡用,但在这种情况下,还是建议你用Serializable。

Focus on technology, enjoy life!—— QQ:804212028
浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值