Android_06_Activity的跳转
来源:互联网 发布:java小程序抽签源代码 编辑:程序博客网 时间:2024/06/08 04:11
MainActivity.java
package com.itheima.jump;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 跳转至打电话activity * 跳转至其他应用的activity * 隐式跳转:通过指定action和data * @param v */ public void click1(View v){ Intent intent = new Intent(); //隐式意图 intent.setAction(Intent.ACTION_CALL); //电话的清单文件的data节点中设置了tel: 所以使用意图设置data的时候,需要加上tel: //对于电话的清单文件,可自行查看源码; intent.setData(Uri.parse("tel:110")); //跳转 startActivity(intent); } /** * 跳转至secondActivity * 在本应用中跳转 * 显示跳转:直接指定目标Activity的包名和类名 * @param v */ public void click2(View v){ Intent intent = new Intent(); //cls:直接指定目标Activity的类名 //显示意图 //在用setClass()方法时,由于SecondActivity是在本应用中,所以类名面前的包名可以省略不写; //当然其类名也可以写成包名+类名的形式,如:com.itheima.jump.SecondActivity.class //intent.setClass(this, SecondActivity.class); //在使用setClassName()方法时,其类名需要用包名+类名的形式; //但是在清单文件中,声明Activity时,可以用 .类名的形式代替包名+类名的形式; intent.setClassName("com.itheima.jump", "com.itheima.jump.SecondActivity"); startActivity(intent); } /** * 显示跳转至拨号器 */ public void click3(View v){ Intent intent = new Intent(); //指定目标Activity的包名和类名,关于系统应用的包名和类名,可以通过打开应用的时候,查看其logcat输出,即可看到; //由于dialer这个应用不在本应用中,所以包名和类名记得写全其路径; intent.setClassName("com.android.dialer", "com.android.dialer.DialtactsActivity"); startActivity(intent); } /** * 隐式跳转至拨号器 */ public void click4(View v){ Intent intent = new Intent(); //隐式设置拨号器的动作 intent.setAction(Intent.ACTION_DIAL); startActivity(intent); } /** * 隐式跳转至secondActivity * * 若想要对自定义的Activity做隐式跳转,需要在清单文件相应的Activity节点上配置intent-filter子节点 * 在intent-filter节点中配置了哪些子节点,那么在代码中设置意图时,需要全都进行相应的设置; * 在intent-filter节点进行设置时,其至少需要配置两个子节点:action和category * 其中,当category节点的值为android.intent.category.DEFAULT * 那么,在代码中,设置意图时,可以将其省略,若category是其他值,则需要在设置意图时相应的写出来; * * 在一个Activity节点中,可以有多个intent-filter子节点;在代码中,设置意图时, * 只需能匹配任意一个intent-filter子节点即可; * 当然,在一个intent-filter节点中,也可以配置多个相同属性的子节点,那么在代码中设置意图时, * 对于多个相同属性的子节点,设置其中对应的一个属性即可; * * 其中,在清单文件中的Activity的intent-filter节点的子节点data中, * 若设置了scheme和mimeType两个属性,那么想要其都匹配,则在设置意图时, * 需要使用setDataAndType() * */ public void click5(View v){ Intent intent = new Intent(); intent.setAction("com.itheima.sa2"); //在用setData和setType都设置意图时,那么两者存活一者, //若想要两者都存活,则需要使用setDataAndType()// intent.setData(Uri.parse("heima2:qwe"));// intent.setType("text/username");// intent.setData(Uri.parse("heima2:qwe123")); intent.setDataAndType(Uri.parse("heima2:qwe123"), "text/username"); //系统会自动添加默认的category,由于在清单文件中,Category的值category.DEFAULT //所以在这里可以不写,因为不写的话,系统会默认添加;当然显式写出来也可以;若是其他值,则必须显式写出来; intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); } /** * 显式跳转至浏览器 */ public void click6(View v){ Intent intent = new Intent(); intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); startActivity(intent); } /** * 隐式跳转至浏览器 * @param v */ public void click7(View v){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent); }}
SecondActivity.java
package com.itheima.jump;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;public class SecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//获取到启动这个activity的意图
<span style="white-space:pre"></span>/*Intent intent = getIntent();Uri uri = intent.getData();System.out.println(uri.toString());<span style="white-space:pre"></span>*/}}
layout
1>
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是主activity" android:textSize="18sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式跳转至打电话activity" android:onClick="click1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示跳转至secondActivity" android:onClick="click2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示跳转至拨号器" android:onClick="click3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式跳转至拨号器" android:onClick="click4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式跳转至secondActivity" android:onClick="click5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示跳转至浏览器" android:onClick="click6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐式跳转至浏览器" android:onClick="click7" /></LinearLayout>
2>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个activity" android:textSize="18sp" /></LinearLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.jump" 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.itheima.jump.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> <!-- 对于 .SecondActivity 的写法,其中 . 表示本应用的包名,所以在清单文件中,可以 --> <activity android:name=".SecondActivity"> <intent-filter > <action android:name="com.itheima.sa"/> <action android:name="com.itheima.sa3"/> <data android:scheme="heima"/> <data android:scheme="heima3"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> <intent-filter > <action android:name="com.itheima.sa2"/> <data android:scheme="heima2" android:mimeType="text/username"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application></manifest>
注:
1>
在创建一个activity时,需要在清单文件中为其配置一个activity标签
标签中如果带有intent-filter这个子节点,则会在系统中多创建一个快捷图标
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
一个应用程序可以在桌面创建多个快捷图标。
activity的名称、图标可以和应用程序的名称、图标不相同
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
2>
显式意图和隐式意图的应用场景:
显式意图常用于启动同一应用中的Activity
隐式意图常用于启动不同应用中的Activity
在隐式意图中, 如果系统中存在多个Activity的intent-filter同时与你的intent匹配,
那么系统会显示一个对话框,列出所有匹配的Activity,由用户选择启动哪一个
0 0
- Android_06_Activity的跳转
- Android_06_Activity跳转时,数据的传递及接收
- Android_06_Activity的生命周期
- Android_06_Activity的四种启动模式
- struts2的服务器跳转、客户端跳转、跳转类型
- 登录时跳转再跳转的道理
- 服务器跳转与客户端跳转的区别
- 客户端跳转和服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 服务器跳转与客户端跳转的区别
- SpringMvc的服务器端跳转和客户端跳转
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 客户端跳转与服务器端跳转的区别
- 十六进制颜色码
- js小妙用
- DOM对象
- java环境配置和tomcat环境配置
- ACE的线程管理机制
- Android_06_Activity的跳转
- eclipse或myeclipse模板设置
- 决策树1---特征选择(信息增益)
- 优秀学习网站一网打尽(想起来记录一下吧)
- iOS UILabel 文字自动左上角对齐
- tomcat指定jre、jdk
- hpux 11.31添加、删除磁盘
- 惠普功能测试UFT(QTP)使用心得
- 在进行软件项目开发相关的软件总结(逐步添加中....)