MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickBtn(View view)
{
int id=view.getId();
if(id==R.id.btn1)
{
//显式打开
// Intent intent=new Intent(this,SecondActivity.class);
// startActivity(intent);
Intent intent=new Intent();
//设定intent要启动的组件名称 这是典型的显式启动
ComponentName cn=new ComponentName(this, SecondActivity.class);
intent.setComponent(cn);
}else if(id==R.id.btn2)
{
//隐式启动Intent 不会直接指明要启动的组件名称
//而是通过给Intent设定的操作(Action)和要传送的数据(Data)来间接指明组件
Intent intent =new Intent();
//使用action指明Intent要执行的操作 这里操作的名称是ACTION_VIEW
//具体的值android.intent.action.VIEW 代表了一个查看的操作
intent.setAction(Intent.ACTION_VIEW);
//这里指明要查看的数据 这个数据的书写格式必须是URI格式
intent.setData(Uri.parse("http://www.sina.com"));
//那么复合这个设定条件的Activity将会被Android启动
//这种启动方式被称之为隐式启动 这种启动方式是最常用的Intent的使用形式
//也是Android推荐的
startActivity(intent);
}else if(id==R.id.btn3)
{
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
//设定复合Uri规范的电话号码
//Android会根据你所设定的Action以及data 和category 综合筛选出复合条件的Activity(组件)
//intent.setData(Uri.parse("email:13830515@qq.com"));
intent.setData(Uri.parse("tel:13874938836"));
startActivity(intent);
}else if(id==R.id.btn4)
{
Intent intent=new Intent();
Log.i("test", "aaaaa");
//同一个Activity可以支持不同的Action
//筛选条件的匹配是依赖于各个组件在manifest.xml中配置时所设定的intent-filter
intent.setAction(Intent.ACTION_EDIT);
intent.setData(Uri.parse("tel:13874938836"));
startActivity(intent);
}else if(id==R.id.btn5)
{
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://localhost:8081/aaaa/a.jpg"));
startActivity(intent);
}else if(id==R.id.btn6)
{
Intent intent=new Intent();
intent.setAction("android.intent.action.My_Test");
//intent.setData(Uri.parse("http://www.sina.com"));
//设置Catagory分类
//intent.setType(type)
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.intent2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=com.intent2.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="com.intent2.SecondActivity"
android:label="@string/title_activity_second" >
</activity>
<activity
android:name="com.intent2.ThirdActivity"
android:label="@string/title_activity_third" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.My_Test"/>
<data android:scheme="tel"/>
<!-- 如果这个Activity想要被隐式打开 那么必须设定category为default -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
<LinearLayout 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"
tools:context=".MainActivity" >
<Button android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="显式打开Activity" />
<Button android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="隐式打开浏览器" />
<Button android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="隐式打开拨号界面" />
<Button android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="隐式打开通话界面" />
<Button android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="隐式打开第3个窗口" />
<Button android:id="@+id/btn6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickBtn"
android:text="隐式打开第3个窗口_自定义Action" />
</LinearLayout>