本文简述在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 }
日历上设置日程
//设置日历事件 public void open_calendar(View v){ Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI); Calendar beginTime = Calendar.getInstance(); beginTime.set(2018, 0, 19, 7, 30); Calendar endTime = Calendar.getInstance(); endTime.set(2018, 0, 19, 10, 30); calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()); calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()); calendarIntent.putExtra(CalendarContract.Events.TITLE, "Ninja class"); calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo"); startActivity(calendarIntent); }
判断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()); 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的用途还有很多,本文旨在抛砖引玉,希望大家共同学习。