案例:
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.example.administrator.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通过组件名称直接查找组件"
android:id="@+id/button_comp"
android:onClick="componentClick"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Action属性查找组件"
android:id="@+id/button_action"
android:onClick="actionClick"
android:layout_below="@+id/button_comp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_comp"
android:layout_alignEnd="@+id/button_comp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用data属性查找组件"
android:id="@+id/button_data"
android:onClick="dataClick"
android:layout_below="@+id/button_action"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_action"
android:layout_alignEnd="@+id/button_action" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="flag属性"
android:id="@+id/button_flag"
android:onClick="flagClick"
android:layout_below="@+id/button_data"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button_data"
android:layout_alignEnd="@+id/button_data" />
</RelativeLayout>
MainActivity.java
package com.example.administrator.myapplication;
import android.app.Activity;
import android.content.ComponentCallbacks2;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*直接查找法(通过组件名称)*/
public void componentClick(View v) {
/* Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,MainActivity2.class);
intent.setComponent(componentName);*/
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
/*使用间接法(通过action属性和category)
* 在Activity的清单文件配置中,必须使用默认的类别
* android.intent.category.DEFAULT*/
public void actionClick(View v) {
// Intent intent=new Intent("com.example.administrator.myapplication.action.MY_ACTION");
Intent intent = new Intent();
intent.setAction("com.example.administrator.myapplication.action.MY_ACTION");
intent.addCategory("com.example.administrator.myapplication.category.MY_CATEGORY");
startActivity(intent);
}
/*data属性,一般与action配合使用(此组合是最常见的使用方法)
* type属性,表示数据的类型*/
public void dataClick(View v) {
Intent intent = new Intent();
// intent.setAction("com.example.administrator.myapplication.action.MY_ACTION");
intent.setAction(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://www.baidu.com");
// intent.setData(data); //会把type属性设置为空
//intent.setType(); // 会把data属性设置为空
//要type和data一起用,必须使用以下方法,匹配时必须两者同时匹配,才能编译通过
intent.setDataAndType(data, "text/html");
startActivity(intent);
}
public void flagClick(View v) {
Intent intent = new Intent(this, MainActivity5.class);
//设置Activity的启动模式
//Intent.FLAG_ACTIVITY_NEW_TASK 在新的任务栈中启动Activity,如果有,那么就在本任务中启动
//Intent.FLAG_ACTIVITY_CLEAR_TASK 相当于singleTask
//Intent.FLAG_ACTIVITY_CLEAR_TOP 相当于singleTop
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
activity_main2.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.example.administrator.myapplication.MainActivity2">
</RelativeLayout>
MainActivity2.java
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
activity_main3.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.example.administrator.myapplication.MainActivity3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="action test"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity3.java
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity3 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
}
activity_main4.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.example.administrator.myapplication.MainActivity4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="data test"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity4.java
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity4 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
}
}
activity_main5.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.example.administrator.myapplication.MainActivity5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="flag test"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity5.java
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity5 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
}
}
清单文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity2"
android:label="@string/title_activity_main2">
<intent-filter android:priority="-1">
<action android:name="com.example.administrator.myapplication.action.MY_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity3"
android:label="@string/title_activity_main3">
<intent-filter android:priority="2">
<action android:name="com.example.administrator.myapplication.action.MY_ACTION" />
<action android:name="com.example.administrator.myapplication.action.MY_ACTION2" />
<action android:name="com.example.administrator.myapplication.action.MY_ACTION3" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.administrator.myapplication.category.MY_CATEGORY" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity4"
android:label="@string/title_activity_main4">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="www.baidu.com"
android:mimeType="text/html"
android:scheme="http" />
</intent-filter>
</activity>
<activity
android:name=".MainActivityA"
android:label="@string/title_activity_main">
</activity>
<activity
android:name=".MainActivityB"
android:label="@string/title_activity_main_activity_b"
android:launchMode="singleInstance" />
<activity
android:name=".MainActivity5"
android:label="@string/title_activity_main5"></activity>
</application>
</manifest>
案例:
清单文件:
<activity
android:name=".MainActivityA"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivityB"
android:label="@string/title_activity_main_activity_b"
android:launchMode="singleInstance" />
activity_main_a.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.example.administrator.myapplication.MainActivityA">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-A"
android:id="@+id/button_a"
android:onClick="startA"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-B"
android:id="@+id/button_b"
android:layout_below="@+id/button_a"
android:layout_alignRight="@+id/button_a"
android:layout_alignEnd="@+id/button_a"
android:onClick="startB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-A"
android:id="@+id/textView4"
android:layout_below="@+id/button_b"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivityA.java
package com.example.administrator.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivityA extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_a);
}
public void startA(View v) {
Intent intent = new Intent(this, MainActivityA.class);
startActivity(intent);
}
public void startB(View v) {
Intent intent = new Intent(this, MainActivityB.class);
startActivity(intent);
}
}
activity_main_b.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.example.administrator.myapplication.MainActivityB">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-A"
android:id="@+id/button_a"
android:onClick="startA"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-B"
android:id="@+id/button_b"
android:layout_below="@+id/button_a"
android:layout_alignRight="@+id/button_a"
android:layout_alignEnd="@+id/button_a"
android:onClick="startB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity-B"
android:id="@+id/textView3"
android:layout_below="@+id/button_b"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivityB.java
package com.example.administrator.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivityB extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_b);
}
public void startA(View v) {
Intent intent = new Intent(this, MainActivityA.class);
startActivity(intent);
}
public void startB(View v) {
Intent intent = new Intent(this, MainActivityB.class);
startActivity(intent);
}
}
Intent应用
activity_main6.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.example.administrator.myapplication.MainActivity6">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示网页"
android:onClick="borwsePageClick"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话"
android:onClick="callPhoneDiaClick"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用发送短信的程序"
android:onClick="smsDiaClick"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:onClick="smsSendClick"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放多媒体"
android:id="@+id/button6"
android:layout_below="@+id/button5"
android:onClick="playMusicClick"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uninstall程序"
android:id="@+id/button7"
android:onClick="uninstallClick"
android:layout_below="@+id/button6"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安装apk"
android:onClick="installClick"
android:id="@+id/button8"
android:layout_below="@+id/button7"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity6.java
package com.example.administrator.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import java.io.File;
public class MainActivity6 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
}
//打开网页示例
public void borwsePageClick(View v)
{
Uri data= Uri.parse("http://www.baidu.com");
String action= Intent.ACTION_VIEW;
Intent intent=new Intent(action,data);
startActivity(intent);
}
//打开拨号面板
public void callPhoneDiaClick(View v)
{
Uri data=Uri.parse("tel:13760359165");
String action=Intent.ACTION_DIAL;
Intent intent=new Intent(action,data);
startActivity(intent);
}
//打开拨号面板
public void callPhoneClick(View v)
{
Uri data=Uri.parse("tel:123456789");
String action=Intent.ACTION_CALL;
Intent intent=new Intent(action,data);
startActivity(intent);
}
//打开短信的面板
public void smsDiaClick(View v)
{
Intent intent =new Intent();
String action=Intent.ACTION_VIEW;
intent.setAction(action);
intent.putExtra("sms_body","你今天吃了吗");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
//发送短信
public void smsSendClick(View v)
{
Uri data=Uri.parse("smsto:123456789");
String action=Intent.ACTION_SENDTO;
Intent intent =new Intent(action,data);
intent.putExtra("sms_body", "一起吃个饭呗");
startActivity(intent);
}
//播放音乐
public void playMusicClick(View v)
{
Uri data=Uri.parse("file:///sdcard/Music/xpg.mp3");
String action=Intent.ACTION_VIEW;
Intent intent=new Intent();
intent.setAction(action);
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
}
//卸载程序
public void uninstallClick(View v)
{
Uri data=Uri.parse("package:com.example.administrator.myapplication");
String action=Intent.ACTION_DELETE;
Intent intent=new Intent(action,data);
startActivity(intent);
}
//安装程序
public void installClick(View v)
{
Uri data=Uri.fromFile(new File("/sdcard/Download/hello.apk"));
String action=Intent.ACTION_VIEW;
Intent intent=new Intent();
intent.setAction(action);
intent.setDataAndType(data,"application/vnd.android.package-archive");
startActivity(intent);
}
}
清单文件:
<activity
android:name=".MainActivity6"
android:label="@string/title_activity_main6">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>