android中Intent学习笔记
来源:互联网 发布:手机多大内存够用 知乎 编辑:程序博客网 时间:2024/05/29 16:40
安卓中Intent学习笔记
假设现在已经有了 FirstActivity和SecondActivity 2个活动 并且在AndroidManifest已经注册了
清单文件
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".FirstActivity" android:label="@string/app_name" > <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>
至于如何进行跳转的逻辑设定,你们自己进行设计。
显示跳转
在点击事件的时候执行,比较简单
btn_b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 参数一时当前活动的引用,第二个参数是要跳转的活动的字节码文件 Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } });
隐示跳转
1.使用默认的category
在 AndroidManifest.xml
的application中需要跳转的activity中添加
<activity android:name=".SecondActivity"> <intent-filter > <action android:name="intent1"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
之后再
//参数取 <action android:name="intent1"/>中的值 Intent intent = new Intent("intent1"); startActivity(intent);
2.使用自定义的category
在 AndroidManifest.xml
的application中需要跳转的activity中添加
<activity android:name=".SecondActivity"> <intent-filter > <action android:name="intent1"/> <!-- 一定要加下面的default 不然报错 --> <category android:name="android.intent.category.DEFAULT"/> <category android:name="cc" /> </intent-filter> </activity>
之后再
//参数取 <action android:name="intent1"/>中的值 Intent intent = new Intent("intent1"); intent.addCategory("cc"); startActivity(intent);
3.调用系统的intent
Button btn = (Button) findViewById(R.id.b); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.baidu.com")); startActivity(intent); } }); Button btn2 = (Button) findViewById(R.id.b2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:110")); startActivity(intent); } });
使用intent的传递数据
1.简单的传递
修改我们上面写的代码
// 参数一时当前活动的引用,第二个参数是要跳转的活动的字节码文件 Intent intent = new Intent(FirstActivity.this,SecondActivity.class); intent.putExtra("d1", "d1"); startActivity(intent);
在SecondActivity中添加,这句话就是得到启动SecondActivity的intent,然后取出数据,根据不同数据类型采用不同的get方法,自己可以查看
Intent i =getIntent(); String s = i.getStringExtra("d1");
2.结束活动传递数据到开启结束活动的活动里
原来都是 活动A 跳到 活动B
如果 B 结束了 根本无法和A 产生关联,所以用下面这个方法去启动B,在B活动中设置一个回调函数,当结束的时候会调用A中的方法
并且传入数据
// 参数一时当前活动的引用,第二个参数是要跳转的活动的字节码文件 Intent intent = new Intent(FirstActivity.this,SecondActivity.class); intent.putExtra("d1", "d1");// startActivity(intent); startActivityForResult(intent, 1);//采用这个方法去启动活动
重写act的一个方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if(resultCode == RESULT_OK){ String stringData = data.getStringExtra("d2"); Log.d("=", stringData); } break; default: break; } }
在SecondAct中添加btn并给他添加事件,之后插入下面代码
Intent intent = new Intent(); intent.putExtra("d2", "d2"); setResult(RESULT_OK,intent); finish();//摧毁act 相当于back键
其实可以复写back按键的方法 如下
@Override public void onBackPressed() { //代码和上面一样,只是重写了 back 返回键的方法 }
0 0
- android中Intent学习笔记
- Android 开发中 Intent 和 Intent 过滤器学习笔记
- Android学习笔记--Intent
- android学习笔记---Intent
- Android学习笔记 - Intent
- Android中Intent笔记
- 【Android学习笔记】Android中Intent的应用方法探索
- 【Android学习笔记】Android中Intent的应用方法探索
- android学习笔记之Intent&Intent filter
- android学习笔记:Intent,Intent Filter
- Pro Android学习笔记(十一):了解Intent(中)
- Pro Android学习笔记(十一):了解Intent(中)
- android学习笔记之Intent
- Android学习笔记 - Intent篇
- Android学习笔记 - Intent篇
- Android 学习笔记(2) -- Intent
- android学习笔记2-intent
- Android学习笔记-Intent(一)
- 使用Google的prettify高亮显示代码
- 数组相关
- 笔记——Texture Synthesis Using Convolutional Neural Networks (Leon A. Gatys et al)
- 104. Maximum Depth of Binary Tree
- leecode 解题总结:363. Max Sum of Rectangle No Larger Than K
- android中Intent学习笔记
- android studio心得
- 替换空格
- 513. Find Bottom Left Tree Value
- ucosii在stm32上的移植详解2
- RVCT安装
- POJ2253 Frogger
- 【Spring】注解
- 手推机器学习公式(一) —— BP 反向传播算法