Intent及其七大属性及intent-filter设置
//第一种方式:Intent intent = new Intent(MainActivity.this,NextActivity.class);startActivity(intent);
//第二种方式:Intent intent = new Intent();intent.setClass(MainActivity.this, NextActivity.class);startActivity(intent);
//其实还有很多种Intent实现页面跳转的写法。
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的时候需要声明: < intent-filter>com.steven.android06lifecycle.nextactivity" />
//当某个页面是默认启动页面时,需要定义Action、Category的属性必须为以下字符串:【 设置任务入口】android.intent.action.MAIN " />android.intent.category.LAUNCHER " />
Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGOTY_HOME);startActivity(intent);
(一)、拨打电话: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);
(四)、调用发送短信的程序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={"[email protected]"};String[] ccs={"[email protected]"};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 = { "[email protected]" };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;
}
}