一起学Android之Intent
程序员文章站
2022-06-25 08:17:09
本文简述在Android开发中Intent的常见应用,仅供学习分享使用。 ......
本文简述在android开发中intent的常见应用,仅供学习分享使用。
什么是intent?
intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,android则根据此intent的描述,负责找到对应的组件,将 intent传递给调用的组件,并完成组件的调用。因此,intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。【官方文档:an intent is an abstract description of an operation to be performed(执行某操作的抽象描述)】
intent的用途有哪些?
关联不同的组件:
- 用来激活启动其他应用程序的组件。
- 作为传递数据和事件的桥梁。
intent调用模式
- 显式intent:直接指定目标组件的componentnamae,适合启动同一个应用中的其他组件,比如在某应用程序内,一个activity启动一个service。
- 隐式intent:不直接指定目标组件的componentname class,适合启动设备中不同应用中的组件。
隐式intent常见例子
打开地图:
1 //打开地图 2 public void open_map(view v) { 3 // map point based on address 4 //uri location = uri.parse("geo:0,0?q=1600+amphitheatre+parkway,+mountain+view,+california"); 5 // or map point based on latitude/longitude 6 uri location = uri.parse("geo:34.9501439901632,114.95770290767824?z=14"); // z param is zoom level 7 intent mapintent = new intent(intent.action_view, location); 8 startactivity(mapintent); 9 }
打开网页:
1 //打开指定的网页 2 public void open_url(view v){ 3 uri webpage = uri.parse("http://www.baidu.com"); 4 intent webintent = new intent(intent.action_view, webpage); 5 startactivity(webintent); 6 }
打电话
1 //打电话 2 public void open_tel(view v){ 3 uri number = uri.parse("tel:10086"); 4 intent callintent = new intent(intent.action_dial, number); 5 startactivity(callintent); 6 }
日历上设置日程
1 //设置日历事件 2 public void open_calendar(view v){ 3 intent calendarintent = new intent(intent.action_insert, calendarcontract.events.content_uri); 4 calendar begintime = calendar.getinstance(); 5 begintime.set(2012, 0, 19, 7, 30); 6 calendar endtime = calendar.getinstance(); 7 endtime.set(2012, 0, 19, 10, 30); 8 calendarintent.putextra(calendarcontract.extra_event_begin_time, begintime.gettimeinmillis()); 9 calendarintent.putextra(calendarcontract.extra_event_end_time, endtime.gettimeinmillis()); 10 calendarintent.putextra(calendarcontract.events.title, "ninja class"); 11 calendarintent.putextra(calendarcontract.events.event_location, "secret dojo"); 12 startactivity(calendarintent); 13 }
判断intent是否有接收app
1 public void open_chkintent(view v){ 2 intent calendarintent = new intent(intent.action_insert, calendarcontract.events.content_uri); 3 calendar begintime = calendar.getinstance(); 4 begintime.set(2012, 0, 19, 7, 30); 5 calendar endtime = calendar.getinstance(); 6 endtime.set(2012, 0, 19, 10, 30); 7 calendarintent.putextra(calendarcontract.extra_event_begin_time, begintime.gettimeinmillis()); 8 calendarintent.putextra(calendarcontract.extra_event_end_time, endtime.gettimeinmillis()); 9 calendarintent.putextra(calendarcontract.events.title, "ninja class"); 10 calendarintent.putextra(calendarcontract.events.event_location, "secret dojo"); 11 packagemanager packagemanager = getpackagemanager(); 12 list<resolveinfo> activities = packagemanager.queryintentactivities(calendarintent, 0); 13 boolean isintentsafe = activities.size() > 0; 14 string msg=isintentsafe?"有合适的接收":"没有合适的接收"; 15 toast.maketext(this,msg,toast.length_short).show(); 16 }
显式调用intent
显式调用并传递参数
1 //打开一个activity 2 public void open_activity_param(view v){ 3 intent intent =new intent(this,otheractivity.class); 4 intent.putextra("name","alan.hsiang"); 5 intent.putextra("age",100); 6 intent.putextra("sex",true); 7 startactivity(intent); 8 }
新的activity获取参数并展示
1 intent intent=getintent(); 2 if(intent!=null){ 3 if( intent.hasextra("name")) { 4 string name = intent.getstringextra("name"); 5 integer age = intent.getintextra("age", 0); 6 boolean sex = intent.getbooleanextra("sex", true); 7 log.i("demointent", "oncreate: "+name+"--"+age+"--"+sex+" "); 8 edittext txtname = (edittext) this.findviewbyid(r.id.txt_name); 9 txtname.settext(name); 10 log.i("demointent", "oncreate: txtname "); 11 edittext txtage = (edittext) this.findviewbyid(r.id.txt_age); 12 txtage.settext(age.tostring());//此处要转换成字符串,否则会被当成id,从而报错 13 log.i("demointent", "oncreate: txtage "); 14 radiobutton rbmale = (radiobutton) this.findviewbyid(r.id.rb_male); 15 radiobutton rbfemale = (radiobutton) this.findviewbyid(r.id.rb_female); 16 log.i("demointent", "oncreate: rbbutton "); 17 rbmale.setchecked(sex); 18 rbfemale.setchecked(!sex); 19 } 20 }
备注
intent的用途还有很多,本文旨在抛砖引玉,希望大家共同学习。