使用Intent在活动之间穿梭
使用Intent在活动之间穿梭
1.在com.example.activitytest中创建第二个活动SecondActivity:
/**
* 第二个活动
*/
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
创建完成后会自动生成second_layout.xml,这里我们进行修改如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
/>
</LinearLayout>
这时AndroidManifest.xml已经帮我们注册了活动:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".FirstActivity"
android:label="this is first">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
2.使用Intent启动活动
Intent是Android中各组件进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据.
Intent大致可以分为两种:显示Intent和隐式Intent
一.显式Intent
Intent中有多个构造函数的重载,其中一个Intent(Context packageContext,Class<?> cls),这个构造函数第一个参数是启动活动的上下文,第二个启动活动的目标.
//启动第二个活动
public void onClick(View v) {
//FirstActivity.this 指上下文 SecondActivity.class指活动目标
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
二.隐式Intent
通过<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,
打开AndroidManifest.xml,添加如下代码:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
修改FirstActivity中按钮的点击事件:
//隐式使用Intent
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}
可以选择添加多个category:
//隐式使用Intent
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
打开AndroidManifest.xml,添加如下代码:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
3.Intent的其他使用方法
跳转第三方链接
//跳转第三方链接
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
调用拨号功能
//调用拨号功能
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
使用Intent在活动之间穿梭的更多相关文章
- 使用Intent在活动之间穿梭(《第一行代码》读书笔记)
以下全是个人理解//瞎扯 其实活动理解理解起来就像一个个函数 那么Intent就是调用函数和参数传递 可以有无参,仅仅是调用 Intent intent = new Intent(A.this, B. ...
- 从0系统学Android-2.3使用 Intent 在 Activity 之间穿梭
2.3 使用 Intent 在 Activity 之间穿梭 在上一节中我们已经学会了如何创建一个 Activity 了.对于一个应用程序来说,肯定不可能只有一个 Activity.下面就来学习多个 A ...
- Android: Intent实现活动之间的交互
Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...
- intent 活动之间穿梭
1.从当前activity,跳转到当前应用程序的activity Intent intent = new Intent(MainActivity.this, Intent2Activity.class ...
- (三)使用Intent在活动中穿梭:显式和隐式Intent
一.显式Intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan ...
- ###Intent的使用(活动中穿梭)
让活动切换有两种方式 显示意图和隐式意图 显示意图:只能在本应用中穿梭: 隐式意图:可以调用其他应用程序的活动,包括系统应用,但是需要配置清单文件 显式Intent 1) 创建一个新的活动 2) 确定 ...
- 我的Android第五章:通过Intent实现活动与活动之间的交互
Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互 同时Inten ...
- intent,实现两个活动之间数据的传递
一.Intent 可以启动一个活动,也可以在启动活动的时候传递数据.intent中提供了putExtra()方法,它可以把我们想要传递的数据暂存在intent中,启动了另一个活动后,通过getInte ...
- 在活动之间切换(显式Intent)
实验名称:在活动之间切换 实验现象:通过点击主活动的按钮进入下一个界面 使用技术:显式Intent 步骤: 1.创建一个项目,加载布局.添加一个button 2.新建一个活动. 3.修改按钮的点击事件 ...
随机推荐
- <%@ include file=""%>与<jsp:include page=""/>
https://www.cnblogs.com/sharpest/p/6117629.html
- html使用技巧
line-height: 27px; /* lineheight和height保持一致就能达到文本垂直居中*/ .top_content li { list-style-image: url(../ ...
- 判断tableVIew滑动的方向
首先设置一个旧的偏移量为0; self.oldContent = 0; - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scr ...
- hdu 2153 仙人球的残影
题目 这道题可以有两种写法: 第一种:找规律,如下: #include <stdio.h> int main() { int n,i,j,res; while (scanf("% ...
- SQL Server 2008 清空删除日志文件(瞬间日志变几M)
sql 在使用中每次查询都会生成日志,但是如果你长久不去清理,可能整个硬都堆满哦,笔者就遇到这样的情况,直接网站后台都进不去了.下面我们一起来学习一下如何清理这个日志吧 SQL2008清空删除日志: ...
- ECG心电图数据1
最近在写一篇基于小波变换的ECG信号压缩算法的论文,遇到了怎样获取ECG信号测试数据的问题,在百度和专业论坛里搜索了一番,发现也有很多朋友为此发愁.现在论文写好了,投稿中,顺便也把怎样获取和处理ECG ...
- Quartz.net 起步
安装 Quartz 程序包 使用 nuget 命令行安装 Quartz: Install-Package Quartz 如果使用 JSON 序列化,使用 nuget 安装 Quartz.Seriali ...
- Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad 方法一:安装单独组件 ...
- 巧用XML配置校验导入Excel的列数据格式
<?xml version="1.0"?> <ColumnsSeting xmlns:xsd="http://www.w3.org/2001/XMLSc ...
- mysql恢复备份错误:Got a packet bigger than 'max_allowed_packet' bytes
最近恢复mysql数据库备份时,出现了一个错误:Got a packet bigger than 'max_allowed_packet' bytes 该问题主要是由于mysql的my.ini文件中设 ...