一、Intent概述及其屬性
Intent基本應用:
- 開啓一個activity
- 開啓一個Service
- 傳遞廣播
Intent對象的屬性:
對於一個Intent對象,實際上是一組被捆綁的信息。
- Component name(組件名稱):設置Intent對象的組件名稱,通過設置Component name可以啓動其他的activity或其他應用的activity,在指定組件名稱時可以通過包名和類名來唯一確定一個activity。
可用**setComponent()**方法進行設置,需傳遞Component name的對象。
創建一個Component name對象需傳遞兩個參數:應用包名,類名(類名中要包括完整包名)。
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//創建一個Intent對象
Intent intent=new Intent();
//創建一個CompnentName對象
ComponentName componentName=new ComponentName("com.mingrisoft.demo2","com.mingrisoft.demo2.DetailActivity");
//設置意圖
intent.setComponent(componentName);
//啓動另一個activity
startActivity(intent);
}
});
- Action和Data
Action屬性:用來指定將要執行的動作。
Data屬性:用來指定具體的數據的。
通常情況下,要一起使用才能表達一個具體的意思。
舉個例子:比如Action爲我想要喝…,喝什麼呢?此時就需要Data指定具體的東西,水?奶茶?等等。
實例:使用Intent實現關於頁面中撥打電話和發送短信的功能。
效果如下:
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.intent_action_data.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="公司:陳小豬和劉大狗" />
<ImageButton
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#0000"
android:src="@drawable/e1"/>
<ImageButton
android:id="@+id/bt1"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/bt2"
android:layout_marginTop="10dp"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/e2"/>
</RelativeLayout>
MainActivity.java文件
package com.mingrisoft.intent_action_data;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton bt1= (ImageButton) findViewById(R.id.bt1);
ImageButton bt2= (ImageButton) findViewById(R.id.bt2);
bt1.setOnClickListener(l);
bt2.setOnClickListener(l);
}
//由於兩個按鈕的事件監聽器比較像,所以可以直接設置一個事件監聽器對象。
View.OnClickListener l=new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
ImageButton imageButton= (ImageButton) view;
switch (imageButton.getId())
{
case R.id.bt1:
intent.setAction(intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0431849876"));
startActivity(intent);
break;
case R.id.bt2:
intent.setAction(intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:0431849876"));
intent.putExtra("sms_body","Welcome to ");
startActivity(intent);
break;
}
}
};
}
manefests文件
<!-- 開啓打電話的權限-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
- Action和Category
兩者經常一起使用,通過它們可調用系統的activity或啓動android系統的其他應用程序組件
Category用來對執行動作類別進行描述。
屬性自行查看API。
實例:使用Intent模擬關閉谷歌地圖返回系統桌面。
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.goodle_intent.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000"
android:textSize="20sp"
android:layout_marginLeft="160dp"
android:layout_marginBottom="70dp"
android:layout_alignParentBottom="true"
android:text="關閉" />
</RelativeLayout>
MainActivity.java文件
package com.mingrisoft.goodle_intent;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Button bt= (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
//作爲初始Activity啓動
intent.setAction(intent.ACTION_MAIN);
//返回桌面
intent.addCategory(intent.CATEGORY_HOME);
//啓動Intent9
startActivity(intent);
}
});
}
}
4.Extras屬性
putExtras()方法:用來把Bundle作爲附加數據進行添加。
getExtras()方法:獲取保存的Bundle信息。
應用場景:
用於多個Activity進行數據交換時。
5.Flag屬性:
①用來指示Android程序如何去啓動另一個Activity,如讓該Activity屬於哪個Task。
Task:以棧的模式聚集在一起的Activity組件的結合。
②指示程序啓動後如何處理,如:指示它是否爲近期的Activity。
同樣參考Android API找提供的常量來設置
//讓當前Activity不在歷史棧中保留,用戶一旦離開他,這個Activity自動關閉
intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);
二、Intent種類
1.顯式Intent
2.隱式Intent
顯式Intent
創建Intent對象的語法格式:
Intent intent=new Intent (Context packageContext,Class<?>cls)
第一個參數一般爲MainActivity.this,第二個參數爲要啓動的Activity的類,例如DeatailActivity.class。
隱式Intent
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//隱式Intent
Intent intent=new Intent();
//把數據顯示給用戶
intent.setAction(intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.mingribook.com"));
startActivity(intent);
}
});
顯式Intent和隱式Intent的區別。
顯式Intent:
- 直接指定目標組件的名稱
- 多用於在應用程序內傳遞信息
隱式Intent:
- 不會用組件名稱定義要激活的目標組件
- 多用於在不同應用程序之間傳遞信息。因爲開發人員不清楚其他應用程序的組件名稱。
三、Intent過濾器
過濾器:一種根據某一個Intent的Action,Data以及Category等屬性,對適合接受這個Intent的組件,進行匹配和篩選的機制。主要使用在使用隱式Intent啓動activity時。
例子:
在ActivityA中調用startActivity()打開另一個目標組件,但不直接指定要打開的組件,而是設置一個過濾器,能通過過濾器的組件即要打開的組件。
設置過濾器
通過<intent-filter 標記在Manifest.xml文件中配置。
<intent-filter>
//指定組件能響應的動作
<action...../>
//指定以哪種方式執行Intent請求的動作
<category...../>
//想action提供要操作的數據
<data...../>
</intent-filter>
常用的兩種過濾器:
1.
2.
實例:在Activity中使用包含預定義動作的隱式Intent啓動另一個Acticity。
效果:
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.lookbigger.MainActivity">
<ImageView
android:layout_centerHorizontal="true"
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="200dp"
android:src="@drawable/t3"/>
<Button
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看大圖"
android:layout_below="@+id/image"/>
</RelativeLayout>
MainActivity.java文件
package com.mingrisoft.lookbigger;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction(intent.ACTION_VIEW);
startActivity(intent);
}
});
}
}
activity_show.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.lookbigger.ShowActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/t3"
android:scaleType="fitXY"/>
</RelativeLayout>
ShowActivity.java文件不改變