织梦做双语网站,商城网页定制开发,重庆妇科医院咨询,html5响应式网站模板目录 Intent对象简述Intent的作用Intent开启Activtiy显式启动Activity隐式启动Activity Intent对象简述
Android的应用程序包含三种重要组件#xff1a;Activity、Service、BroadcastReceiver#xff0c;应用程序采用了一致的方式来启动它们——都是依靠Intent来启动的… 目录 Intent对象简述Intent的作用Intent开启Activtiy显式启动Activity隐式启动Activity Intent对象简述
Android的应用程序包含三种重要组件Activity、Service、BroadcastReceiver应用程序采用了一致的方式来启动它们——都是依靠Intent来启动的Intent就封装了程序想要启动的程序的意图。不仅如此Intent还可用于与被启动组件交换信息。 下图显示了使用Intent启动不同组件的方法
Intent的作用
Intent是一个可以消息传递对象可以通过它来进行组件之间的信息传递。Intent主要有以下三个作用
1开启一个activity
2 开启一个service
3 发送广播消息
Intent开启Activtiy
在操作activity的显式启动和隐式启动之前我们还是需要先了解一下什么是activity的显示启动和隐式启动 显式启动指明要启动的Activity所在的类指的是它已经明确指定了将要启动的组件故称作显式启动 隐式启动系统根据Intent的动作和数据来决定启动那个Activity指的是没有指定Component属性没有明确指定要启动哪个组件引用将会根据Intent指定的规则去启动符合条件的组件但具体是哪个组件不确定 优点只要知道被启动Activity的Action和Category即可不用知道对应的类名或者是包名。 只要Activity有对应的action和Category都会被启动起来。然后提供给用户选择要启动哪一个。 需要被启动的Activity需要在自己的AndroidManifest.xml定义对应的action 和 category。
显式启动Activity
显式启动Activity比较简单先创建一个Intent指定应用程序上下文和需要启动的Activity然后调用startActivity来启动新的Activity
!--AndroidManifest.xml--
activity android:name.SecondActivity/activity
//启动Activity
Intent it new Intent(this, SecondActivity.class);
startActivity(it);
通过类名类启动Activity 一般是同一个APK里面使用 private void startSecondActivityByClass() {XLog.i(TAG, startSecondActivityByClass());Intent intent new Intent(FirstActivity.this, SecondActivity.class);try {startActivity(intent);} catch (Exception e) {XLog.i(TAG, start activity error!);}}通过包名加类名启动 不足被启动的应用的包名或者类名发生变化后就会无法启动。private void startSecondActivityByPackageName() {XLog.i(TAG, startSecondActivityByPackage());Intent intent new Intent();intent.setClassName(getPackageName(), getPackageName() .SecondActivity);try {startActivity(intent);} catch (Exception e) {XLog.i(TAG, start activity error!);}}通过ComponentName启动 不足被启动的应用的包名或者类名发生变化后就会无法启动。 private void startSecondActivityByComponent() {XLog.i(TAG, startSecondActivityByComponent());Intent intent new Intent();intent.setComponent(new ComponentName(getPackageName(), getPackageName() .SecondActivity));try {startActivity(intent);} catch (Exception e) {XLog.i(TAG, start activity error!);}}隐式启动Activity
打开AndroidManifest.xml查看里面的代码在学习隐式启动Activity前需要了解里的参数。
Activity的别名可以修改自己Activity的名称 android.intent.action.MAIN决定应用程序最先启动的Activity
给Activity进行分组可以自己添加一个组别 一种默认的category在调用startActivity()方法的时候会自动将这个category添加到Intent中 如果自己定义的某个Activity要通过隐式启动在AndroidManifast.xml必须加上android.intent.category.DEFAULT否则不起作用
android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里 想要App隐藏图标去掉LAUNCHER或者使用DEFAULT即可
如果一个应用多个Activity都设置了这个参数则程序列表里会显示多个该应用Activity的图标
1.通过Activity的别名隐式启动
activity android:name.SecondActivityintent-filter!--取别名--action android:namecom.example.test.ACTION_START/category android:nameandroid.intent.category.DEFAULT//intent-filter
/activity//隐式启动系统Activity
//参数1字符串某Activity的别名
Intent it new Intent(com.example.test.ACTION_START);
startActivity(it);
2.通过别名和自定义的种类隐式启动
activity android:name.SecondActivityintent-filter action android:namecom.example.test.ACTION_START/category android:nameandroid.intent.category.DEFAULT/category android:namecom.example.test.MY_CATEGORY//intent-filter
/activity
Intent it new Intent(com.example.test.ACTION_START);
//添加自定义的种类
intent.addCategory(com.example.test.MY_CATEGORY);
startActivity(it);
3.根据用户的数据类型打开相应的Activity Intent.ACTION_VIEW是Android系统内置的一个动作通过URi.parse()方法解析后再调用Intent.setData()方法讲这个Un对象传递进去
Intent it new Intent(Intent.ACTION_VIEW);
it.setData(Uri.parse(https://www.baidu.com));
startActivity(it);//参数1字符串某Activity的别名
//参数2打开的路径,通过协议来具体的确定打开什么Activity
Intent it2 new Intent(Intent.ACTION_VIEW, Uri.parse(https://www.baidu.com));
startActivity(it2);Intent it3 new Intent(Intent.ACTION_DIAL, Uri.parse(tel:18812341234));
startActivity(it3);
4.响应用户指定的数据类型
activity android:name.SecondActivityintent-filter tools:ignoreAppLinkUrlError action android:namecom.example.test.ACTION_START/category android:nameandroid.intent.category.DEFAULT/data android:schemehttps/intent-filter
/activity
这样就可以通过Android:scheme指定了数据的协议必须的httos协议这样此Activity就能和浏览器网页 一样响应一个打开网页的Intent了。 文章转载自: http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.cwqrj.cn.gov.cn.cwqrj.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.gbsfs.com.gov.cn.gbsfs.com http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.cjcry.cn.gov.cn.cjcry.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.yqgbw.cn.gov.cn.yqgbw.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.gnfkl.cn.gov.cn.gnfkl.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.sjqml.cn.gov.cn.sjqml.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn