700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Activity详解 Intent显式跳转和隐式跳转 及多个Activity之间传值 总结

Activity详解 Intent显式跳转和隐式跳转 及多个Activity之间传值 总结

时间:2021-07-23 21:15:46

相关推荐

Activity详解 Intent显式跳转和隐式跳转  及多个Activity之间传值 总结

Activity 生命周期

显式 Intent 调用

1//创建一个显式的Intent对象(方法一:在构造函数中指定)2Intentintent=newIntent(Intent_Demo1.this,Intent_Demo1_Result1.class);34Bundlebundle=newBundle();5bundle.putString("id",strID);6intent.putExtras(bundle);78intent.putExtra("name","bbb");9intent.putExtra("userInfo",newUserInfo(1,"name"));10startActivity(intent);1112//创建一个显式的Intent对象(方法二:用setClass方法)13Intentintent=newIntent();14Bundlebundle=newBundle();15bundle.putString("id",strID);16intent.setClass(Intent_Demo1.this,Intent_Demo1_Result1.class);17intent.putExtras(bundle);18startActivity(intent);1920//创建一个显式的Intent对象(方法三:用setClass方法)21Intentintent=newIntent();22Bundlebundle=newBundle();23bundle.putString("id",strID);24intent.setClassName(Intent_Demo1.this,"com.great.activity_intent.Intent_Demo1_Result1");25intent.putExtras(bundle);26startActivity(intent);2728//创建一个显式的Intent对象(方法四:用setComponent方法)29Intentintent=newIntent();30Bundlebundle=newBundle();31bundle.putString("id",strID);32//setComponent方法的参数:ComponentName33intent.setComponent(newComponentName(Intent_Demo1.this,Intent_Demo1_Result1.class));34intent.putExtras(bundle);35startActivity(intent);

Intent隐式跳转 Action

1//创建一个隐式的Intent对象:Action动作2/**3*这里指定的是AndroidManifest.xml文件中配置的4*<intent-filter>标签中的<actionandroid:name="com.great.activity_intent.Intent_Demo1_Result3"/>5*所在的Activity,注意这里都要设置<categoryandroid:name="android.intent.category.DEFAULT"/>6*/7Intentintent=newIntent();8//设置Intent的动作9intent.setAction("com.great.activity_intent.Intent_Demo1_Result3");10Bundlebundle=newBundle();11bundle.putString("id",strID);12intent.putExtras(bundle);13startActivity(intent);

AndroidManifest.xml

1<activityandroid:name="Intent_Demo1_Result3"2android:label="Intent_Demo1_Result3">3<intent-filter>4<actionandroid:name="com.great.activity_intent.Intent_Demo1_Result3"/>5<categoryandroid:name="android.intent.category.DEFAULT"/>6</intent-filter>7</activity>

Category 类别

1//创建一个隐式的Intent对象:Category类别2Intentintent=newIntent();3intent.setAction("com.great.activity_intent.Intent_Demo1_Result33");4/**5*不指定Category或只指定AndroidManifest.xml文件中<intent-filter>标签中配置的任意一个Category6*<categoryandroid:name="android.intent.category.DEFAULT"/>除外,就可以访问该Activity,7*/8intent.addCategory(Intent.CATEGORY_INFO);9intent.addCategory(Intent.CATEGORY_DEFAULT);10Bundlebundle=newBundle();11bundle.putString("id",strID);12intent.putExtras(bundle);13startActivity(intent);

AndroidManifest.xml

<activityandroid:name="Intent_Demo1_Result2"android:label="Intent_Demo1_Result2"><intent-filter><categoryandroid:name="android.intent.category.INFO"/><categoryandroid:name="android.intent.category.BROWSABLE"/><categoryandroid:name="android.intent.category.DEFAULT"/></intent-filter></activity>

Date 数据 跳转

1//创建一个隐式的Intent对象,方法四:Date数据2Intentintent=newIntent();3Uriuri=Uri.parse(":8080/folder/subfolder/etc/abc.pdf");45//注:setData、setDataAndType、setType这三种方法只能单独使用,不可共用6//要么单独以setData方法设置URI7//intent.setData(uri);8//要么单独以setDataAndType方法设置URI及mimetype9intent.setDataAndType(uri,"text/plain");10//要么单独以setType方法设置Type11//intent.setType("text/plain");1213/**14*不指定Category或只指定AndroidManifest.xml文件中<intent-filter>标签中配置的任意一个Category15*<categoryandroid:name="android.intent.category.DEFAULT"/>除外,就可以访问该Activity16*/17Bundlebundle=newBundle();18bundle.putString("id",strID);19intent.putExtras(bundle);20startActivity(intent);

AndroidManifest.xml

1<activityandroid:name="Intent_Demo1_Result2"2android:label="Intent_Demo1_Result2">3<intent-filter>4<categoryandroid:name="android.intent.category.DEFAULT"/>5<data6android:scheme="http"7android:host=""8android:port="8080"9android:pathPattern=".*pdf"10android:mimeType="text/plain"/>11</intent-filter>12</activity>13

调用系统的的组件

//web浏览器Uriuri=Uri.parse(":8080/p_w_picpath/a.jpg");Intentintent=newIntent(Intent.ACTION_VIEW,uri);startActivity(intent);//地图(要在Android手机上才能测试)Uriuri=Uri.parse("geo:38.899533,-77.036476");Intentintent=newIntent(Intent.ACTION_VIEW,uri);startActivity(intent);//路径规划Uriuri=Uri.parse("/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");Intentit=newIntent(Intent.ACTION_VIEW,uri);startActivity(it);//拨打电话-调用拨号程序Uriuri=Uri.parse("tel:15980665805");Intentintent=newIntent(Intent.ACTION_DIAL,uri);startActivity(intent);//拨打电话-直接拨打电话//要使用这个必须在配置文件中加入<uses-permissionandroid:name="android.permission.CALL_PHONE"/>Uriuri=Uri.parse("tel:15980665805");Intentintent=newIntent(Intent.ACTION_CALL,uri);startActivity(intent);//调用发送短信程序(方法一)Uriuri=Uri.parse("smsto:15980665805");Intentintent=newIntent(Intent.ACTION_SENDTO,uri);intent.putExtra("sms_body","TheSMStext");startActivity(intent);//调用发送短信程序(方法二)Intentintent=newIntent(Intent.ACTION_VIEW);intent.putExtra("sms_body","TheSMStext");intent.setType("vnd.android-dir/mms-sms");startActivity(intent);//发送彩信Uriuri=Uri.parse("content://media/external/p_w_picpaths/media/23");Intentintent=newIntent(Intent.ACTION_SEND);intent.putExtra("sms_body","sometext");intent.putExtra(Intent.EXTRA_STREAM,uri);intent.setType("p_w_picpath/png");startActivity(intent);//发送Email(方法一)(要在Android手机上才能测试)Uriuri=Uri.parse("mailto:zhangsan@");Intentintent=newIntent(Intent.ACTION_SENDTO,uri);startActivity(intent);//发送Email(方法二)(要在Android手机上才能测试)Intentintent=newIntent(Intent.ACTION_SENDTO);intent.setData(Uri.parse("mailto:zhangsan@"));intent.putExtra(Intent.EXTRA_SUBJECT,"这是标题");intent.putExtra(Intent.EXTRA_TEXT,"这是内容");startActivity(intent);//发送Email(方法三)(要在Android手机上才能测试)Intentintent=newIntent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_EMAIL,"me@");intent.putExtra(Intent.EXTRA_SUBJECT,"这是标题");intent.putExtra(Intent.EXTRA_TEXT,"这是内容");intent.setType("text/plain");//选择一个邮件客户端startActivity(Intent.createChooser(intent,"ChooseEmailClient"));//发送Email(方法四)(要在Android手机上才能测试)Intentintent=newIntent(Intent.ACTION_SEND);//收件人String[]tos={"to1@","to2@"};//抄送人String[]ccs={"cc1@","cc2@"};//密送人String[]bcc={"bcc1@","bcc2@"};intent.putExtra(Intent.EXTRA_EMAIL,tos);intent.putExtra(Intent.EXTRA_CC,ccs);intent.putExtra(Intent.EXTRA_BCC,bcc);intent.putExtra(Intent.EXTRA_SUBJECT,"这是标题");intent.putExtra(Intent.EXTRA_TEXT,"这是内容");intent.setType("message/rfc822");startActivity(Intent.createChooser(intent,"ChooseEmailClient"));//发送Email且发送附件(要在Android手机上才能测试)Intentintent=newIntent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_SUBJECT,"Theemailsubjecttext");intent.putExtra(Intent.EXTRA_STREAM,"file:///sdcard/mp3/醉红颜.mp3");intent.setType("audio/mp3");startActivity(Intent.createChooser(intent,"ChooseEmailClient"));//播放媒体文件(android对中文名的文件支持不好)Intentintent=newIntent(Intent.ACTION_VIEW);//Uriuri=Uri.parse("file:///sdcard/zhy.mp3");Uriuri=Uri.parse("file:///sdcard/a.mp3");intent.setDataAndType(uri,"audio/mp3");startActivity(intent);Uriuri=Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");Intentintent=newIntent(Intent.ACTION_VIEW,uri);startActivity(intent);//音乐选择器//它使用了Intent.ACTION_GET_CONTENT和MIME类型来查找支持audio/*的所有DataPicker,允许用户选择其中之一Intentintent=newIntent(Intent.ACTION_GET_CONTENT);intent.setType("audio/*");//Intent.createChooser:应用选择器,这个方法创建一个ACTION_CHOOSERIntentstartActivity(Intent.createChooser(intent,"选择音乐"));Intentintent1=newIntent(Intent.ACTION_GET_CONTENT);intent1.setType("audio/*");Intentintent2=newIntent(Intent.ACTION_CHOOSER);intent2.putExtra(Intent.EXTRA_INTENT,intent1);intent2.putExtra(Intent.EXTRA_TITLE,"aaaa");startActivity(intent2);////设置壁纸//Intentintent=newIntent(Intent.ACTION_SET_WALLPAPER);//startActivity(Intent.createChooser(intent,"设置壁纸"));//卸载APK//fromParts方法//参数1:URI的scheme//参数2:包路径//参数3:Uriuri=Uri.fromParts("package","com.great.activity_intent",null);Intentintent=newIntent(Intent.ACTION_DELETE,uri);startActivity(intent);//安装APK(???)Uriuri=Uri.fromParts("package","com.great.activity_intent",null);Intentintent=newIntent(Intent.ACTION_PACKAGE_ADDED,uri);startActivity(intent);//调用搜索Intentintent=newIntent();intent.setAction(Intent.ACTION_WEB_SEARCH);intent.putExtra(SearchManager.QUERY,"android");startActivity(intent);

多个Activity之间传值可以通过Bundle对象存储需要传递的数据,例如:

在IntentDemoActivity里面传值,

IntentexplicitIntent=newIntent(IntentDemoActivity.this,ExplicitActivity.class);//这是在Intent的构造函数中指定EditTextnameText=(EditText)findViewById(R.id.username);//通过Bundle对象存储需要传递的数据Bundlebundle=newBundle();bundle.putString("userName",nameText.getText().toString());//把Bundle对象bundle给explicitIntentexplicitIntent.putExtras(bundle);startActivity(explicitIntent);

在ExplicitActivity获取值:

//获取Intent中的Bundle对象Bundlebundle=this.getIntent().getExtras();//获取Bundle中的数据,注意类型和keyStringuserName=bundle.getString("userName");

两个个Activity之间切换

在ExplicitActivity页面上加一个返回按钮,并在事件写如下代码:/*给上一个Activity返回结果*/Intentintent=newIntent(ExplicitActivity.this,IntentDemoActivity.class);//这是在Intent的构造函数中指定ExplicitActivity.this.setResult(RESULT_OK,intent);/*结束本Activity*/ExplicitActivity.this.finish();这样就返回到IntentDemoActivity这个Activity去了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。