一起学Android之Intent

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述一起学Android之Intent相关的知识,希望能为你提供帮助。
本文简述在android开发中Intent的常见应用,仅供学习分享使用。
什么是Intent?【一起学Android之Intent】Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。【官方文档:An intent is an abstract description of an operation to be performed(执行某操作的抽象描述)】
Intent的用途有哪些?关联不同的组件:

  1. 用来激活启动其他应用程序的组件。
  2. 作为传递数据和事件的桥梁。
Intent调用模式
  1. 显式Intent:直接指定目标组件的ComponentNamae,适合启动同一个应用中的其他组件,比如在某应用程序内,一个Activity启动一个Service。
  2. 隐式Intent:不直接指定目标组件的ComponentName Class,适合启动设备中不同应用中的组件。
隐式Intent常见例子打开地图:
1//打开地图 2public 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 6Uri location = Uri.parse("geo:34.9501439901632,114.95770290767824?z=14"); // z param is zoom level 7Intent mapIntent = new Intent(Intent.ACTION_VIEW, location); 8startActivity(mapIntent); 9}

打开网页:
1//打开指定的网页 2public void open_url(View v){ 3Uri webpage = Uri.parse("http://www.baidu.com"); 4Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage); 5startActivity(webIntent); 6}

打电话
1 //打电话 2public void open_tel(View v){ 3Uri number = Uri.parse("tel:10086"); 4Intent callIntent = new Intent(Intent.ACTION_DIAL, number); 5startActivity(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){ 2Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI); 3Calendar beginTime =Calendar.getInstance(); 4beginTime.set(2012, 0, 19, 7, 30); 5Calendar endTime = Calendar.getInstance(); 6endTime.set(2012, 0, 19, 10, 30); 7calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()); 8calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()); 9calendarIntent.putExtra(CalendarContract.Events.TITLE, "Ninja class"); 10calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo"); 11PackageManager packageManager = getPackageManager(); 12List< ResolveInfo> activities = packageManager.queryIntentActivities(calendarIntent, 0); 13boolean isIntentSafe = activities.size() > 0; 14String msg=isIntentSafe?"有合适的接收":"没有合适的接收"; 15Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); 16}

显式调用Intent显式调用并传递参数
1 //打开一个Activity 2public void open_activity_param(View v){ 3Intent intent =new Intent(this,OtherActivity.class); 4intent.putExtra("Name","Alan.hsiang"); 5intent.putExtra("Age",100); 6intent.putExtra("Sex",true); 7startActivity(intent); 8}

新的Activity获取参数并展示
1Intent intent=getIntent(); 2if(intent!=null){ 3if( intent.hasExtra("Name")) { 4String name = intent.getStringExtra("Name"); 5Integer age = intent.getIntExtra("Age", 0); 6Boolean sex = intent.getBooleanExtra("Sex", true); 7Log.i("DemoIntent", "onCreate: "+name+"--"+age+"--"+sex+" "); 8EditText txtName = (EditText) this.findViewById(R.id.txt_name); 9txtName.setText(name); 10Log.i("DemoIntent", "onCreate: txtName "); 11EditText txtAge = (EditText) this.findViewById(R.id.txt_age); 12txtAge.setText(age.toString()); 13Log.i("DemoIntent", "onCreate: txtAge "); 14RadioButton rbMale = (RadioButton) this.findViewById(R.id.rb_male); 15RadioButton rbFemale = (RadioButton) this.findViewById(R.id.rb_female); 16Log.i("DemoIntent", "onCreate: rbButton "); 17rbMale.setChecked(sex); 18rbFemale.setChecked(!sex); 19} 20}

 
备注  Intent的用途还有很多,本文旨在抛砖引玉,希望大家共同学习。
 

    推荐阅读