本文簡述在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的用途還有很多,本文旨在拋磚引玉,希望大家共同學習。