//第一种方式:Intent intent = new Intent(MainActivity.this,NextActivity.class);startActivity(intent);
//第二种方式:Intent intent = new Intent();intent.setClass(MainActivity.this, NextActivity.class);startActivity(intent);
//其实还有很多种Intent实现页面跳转的写法。
- startActivity()
- startActivtyForResult()
- ComponetName startService()
- boolean bindService()
- sendBroadcast()
- sendOrderedBroadcast ()
- sendStickyBroadcast()
- sendStickyOrderedBroadcast()
- Action作为标识符,代表一个Intent,当一个Activity需要外部协助处理时,就会发出一个Intent,如果一个程序能完成相应功能,只要在intent-filter加上这个这个intent就可以了。
- Data保存需要传递的数据格式,比如:tel://
- Extras保存需要传递的额外数据。
- Category表示Intent的种类,从android上启动Activity有多种方式,比如 程序列表、桌面图标、点击Home激活的桌面等等,Category则用来标识这些Activity的图标会出现在哪些启动的上下文环境里。
Intent intent = new Intent();ComponentName cName = new ComponentName(MainActivity.this,NextActivity.class);intent.setComponent(cName);startActivity(intent);
//实际上,以上的写法都被简化为以下写法:Intent intent = new Intent(MainActivity.this,NextActivity.class);startActivity(intent);
//也就是说,平时我们最常用的Intent页面跳转的写法就调用的是显式Intent。
此外, ComponentName属性可以实现一个app跳转到另一个app。
Intent intent = new Intent();ComponentName cName = new ComponentName("com.steven.testasyncloader.sqlitedata","com.steven.testasyncloader.sqlitedata.MainActivity");//其中两个参数的含义:第一个是要跳转到的app的包名,第二个参数是该包中的要跳转到app的页面的classintent.setComponent(cName);startActivity(intent);
Intent intent = new Intent();intent.setAction("com.steven.android06lifecycle.nextactivity");startActivity(intent);
//在配置文件中注册Activity的时候需要声明:<activity android:name="com.steven.android06lifecycle.NextActivity">< intent-filter><action android:name=" com.steven.android06lifecycle.nextactivity" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
//当某个页面是默认启动页面时,需要定义Action、Category的属性必须为以下字符串:【 设置任务入口】<intent-filter><action android:name="android.intent. action.MAIN " /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
- ACTION_MAIN:(android.intent.action.MAIN)Android程序入口。
- 每个Android应用必须且只能包含一个此类型的Action声明。【如果设置多个,则哪个在前,执行哪个。】
- ACTION_VIEW: (android.intent.action.VIEW) 显示指定数据。
- ACTION_EDIT: (android.intent.action.EDIT) 编辑指定数据。
- ACTION_DIAL: (android.intent.action.DIAL) 显示拨号面板。
- ACTION_CALL: (android.intent.action.CALL) 直接呼叫Data中所带的号码。
- ACTION_ANSWER: (android.intent.action.ANSWER) 接听来电。
- ACTION_SEND: (android.intent.action.SEND) 向其他人发送数据(例如:彩信/email)。
- ACTION_SENDTO: (android.intent.action.SENDTO) 向其他人发送短信。
- ACTION_SEARCH: (android.intent.action.SEARCH) 执行搜索。
- ACTION_GET_CONTENT: (android.intent.action.GET_CONTENT) 让用户选择数据,并返回所选数据。
Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGOTY_HOME);startActivity(intent);
- CATEGORY_DEFAULT: (android.intent.category.DEFAULT) Android系统中默认的执行方式,按照普通Activity的执行方式执行。
- CATEGORY_HOME: (android.intent.category.HOME) 设置该组件为Home Activity。
- CATEGORY_PREFERENCE: (android.intent.category.PREFERENCE) 设置该组件为Preference。
- CATEGORY_LAUNCHER: (android.intent.category.LAUNCHER) 设置该组件为在当前应用程序启动器中优先级最高的Activity,通常与入口ACTION_MAIN配合使用。
- CATEGORY_BROWSABLE: (android.intent.category.BROWSABLE) 设置该组件可以使用浏览器启动。
- tel://:号码数据格式,后跟电话号码。
- mailto://:邮件数据格式,后跟邮件收件人地址。
- smsto://:短息数据格式,后跟短信接收号码。
- content://:内容数据格式,后跟需要读取的内容。
- file://:文件数据格式,后跟文件路径。
- market://search?q=pname:pkgname:市场数据格式,在Google Market里搜索包名为pkgname的应用。
- geo://latitude, longitude:经纬数据格式,在地图上显示经纬度所指定的位置。
(一)、拨打电话:Intent intent=new Intent();intent.setAction(Intent.ACTION_CALL);//intent.setAction("android.intent.action.CALL"); //以下各项皆如此,都有两种写法。intent.setData(Uri.parse("tel:1320010001"));startActivity(intent);
//调用拨号面板:Intent intent=new Intent();intent.setAction(Intent.ACTION_DIAL);intent.setData(Uri.parse("tel:1320010001"));startActivity(intent);
//调用拨号面板:Intent intent=new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setData(Uri.parse("tel:1320010001"));startActivity(intent);
(二)、利用Uri打开浏览器、打开地图等:Uri uri = Uri.parse("http://www.google.com"); //浏览器Uri uri=Uri.parse("geo:39.899533,116.036476"); //打开地图定位Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setData(uri);startActivity(intent);
(三)、播放视频:Intent intent = new Intent();Uri uri = Uri.parse("file:///sdcard/media.mp4");intent.setAction(Intent.ACTION_VIEW);intent.setDataAndType(uri, "video/*");startActivity(intent);
- EXTRA_BCC:存放邮件密送人地址的字符串数组。
- EXTRA_CC:存放邮件抄送人地址的字符串数组。
- EXTRA_EMAIL:存放邮件地址的字符串数组。
- EXTRA_SUBJECT:存放邮件主题字符串。
- EXTRA_TEXT:存放邮件内容。
- EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent的按键。
- EXTRA_PHONE_NUMBER:存放调用ACTION_CALL时的电话号码。
(四)、调用发送短信的程序Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setType("vnd.android-dir/mms-sms");intent.putExtra("sms_body", "信息内容...");startActivity(intent);
//发送短信息Uri uri = Uri.parse("smsto:13200100001");Intent intent = new Intent();intent.setAction(Intent. ACTION_SENDTO );intent.setData(uri);intent.putExtra("sms_body", "信息内容...");startActivity( intent );
//发送彩信,设备会提示选择合适的程序发送Uri uri = Uri.parse("content://media/external/images/media/23"); //设备中的资源(图像或其他资源)Intent intent = new Intent();intent.setAction(Intent. ACTION_SEND );intent.setType("image/png");intent.putExtra("sms_body", "内容");intent.putExtra(Intent.EXTRA_STREAM, uri);startActivity(it);
(五)、发送Email:Intent intent=new Intent();intent.setAction(Intent. ACTION_SEND );String[] tos={"android1@163.com"};String[] ccs={"you@yahoo.com"};intent.putExtra(Intent.EXTRA_EMAIL, tos);intent.putExtra(Intent.EXTRA_CC, ccs);intent.putExtra(Intent.EXTRA_TEXT, "The email body text");intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");intent.setType("message/rfc822");startActivity(Intent.createChooser(intent, "Choose Email Client"));
Intent intent = new Intent(Intent.ACTION_SEND);String[] tos = { "mobileservice@ablesky.com" };intent.putExtra(Intent.EXTRA_EMAIL, tos);intent.putExtra(Intent.EXTRA_TEXT, getPhoneParameter());intent.putExtra(Intent.EXTRA_SUBJECT, "Android日志");intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cacheDir));intent.setType("message/rfc882");intent.setType("plain/text");Intent.createChooser(intent, "请选择邮件发送软件");startActivity(intent);
intent.setAction(android.provider.Settings.ACTION_SETTINGS);
//选择图片 requestCode 返回的标识Intent intent = new Intent();intent.setAction(Intent. ACTION_GET_CONTENT );intent.setType( "image/*" );Intent wrapperIntent = Intent.createChooser(intent, null);startActivityForResult(wrapperIntent, requestCode);
//添加音频Intent intent = new Intent();intent.setAction(Intent. ACTION_GET_CONTENT );intent.setType( "video/*" );Intent wrapperIntent = Intent.createChooser(intent, null);startActivityForResult(wrapperIntent, requestCode);
//视频Intent intent = new Intent();intent.setAction(Intent. ACTION_GET_CONTENT );intent.setType( "video/*" );Intent wrapperIntent = Intent.createChooser(intent, null);startActivityForResult(wrapperIntent, requestCode);
//录音Intent intent = new Intent();intent.setAction(Intent. ACTION_GET_CONTENT );intent.setType( "audio/amr" );intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder");startActivityForResult(intent, requestCode);
Intent intent = new Intent(this, MainActivity.class); //将Activity栈中处于MainActivity主页面之上的Activity都弹出。 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_main_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="直接拨号"/>
<Button
android:id="@+id/button_main_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="启动拨号面板"/>
<Button
android:id="@+id/button_main_dialer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="显示拨号面板"/>
<Button
android:id="@+id/button_main_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="发送短信"/>
<Button
android:id="@+id/button_main_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="系统设置"/>
<Button
android:id="@+id/button_main_datesetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="日期设置"/>
<Button
android:id="@+id/button_main_soundsetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="声音设置"/>
<Button
android:id="@+id/button_main_wifisetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="WIFI设置"/>
<Button
android:id="@+id/button_main_web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="浏览网页"/>
<Button
android:id="@+id/button_main_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="查看联系人"/>
<Button
android:id="@+id/button_main_showimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="查看图片"/>
<Button
android:id="@+id/button_main_showtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="查看文本"/>
<Button
android:id="@+id/button_main_playvideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="播放视频"/>
<Button
android:id="@+id/button_main_playaudio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="播放音频"/>
<Button
android:id="@+id/button_main_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="HOME"/>
</LinearLayout>
</ScrollView>
publicclass MainActivity extends Activity {
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoid clickButton(View view) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
switch (view.getId()) {
case R.id.button_main_call:
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086"));
break;
case R.id.button_main_dial:
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
break;
case R.id.button_main_dialer:
intent.setAction("com.android.phone.action.TOUCH_DIALER");
break;
case R.id.button_main_sms:
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
intent.putExtra("sms_body", "该吃饭了,下课吧!");
break;
case R.id.button_main_setting:
intent.setAction("android.settings.SETTINGS");
break;
case R.id.button_main_datesetting:
intent.setAction("android.settings.DATE_SETTINGS");
break;
case R.id.button_main_soundsetting:
intent.setAction("android.settings.SOUND_SETTINGS");
break;
case R.id.button_main_wifisetting:
intent.setAction("android.settings.WIFI_SETTINGS");
break;
case R.id.button_main_contacts:
intent.setAction("com.android.contacts.action.LIST_CONTACTS");
break;
case R.id.button_main_web:
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
break;
case R.id.button_main_showimage:
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File("mnt/sdcard/Download/landscape.jpg")),
"image/*");
break;
case R.id.button_main_showtext:
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File("mnt/sdcard/Download/info.txt")),
"text/*");
break;
case R.id.button_main_playaudio:
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(
"mnt/sdcard/Download/heavencity.mp3")), "audio/*");
break;
case R.id.button_main_playvideo:
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File("mnt/sdcard/Download/girl.3gp")),
"video/*");
break;
case R.id.button_main_home:
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
break;
default:
break;
}
startActivity(intent);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}