Intent(8.19)
来源:互联网 发布:php相册源码 编辑:程序博客网 时间:2024/05/22 15:48
- Intent
- 启动另一个服务
- 显示的Intent
- 隐式的Intent
- 高级用途可以启动其他活动
- 向下一个活动传递数据
- 返回数据给上一个对象
- 启动另一个服务
- Android 四大组件
- intent用于android中通讯的工具
Intent
启动另一个服务
显示的Intent
- 创建第二个活动的xml
- 创建第二个活动,在AndroidManifest中注册
<activity android:name=".SecondActivity" ></activity>
- 活动的启动,
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
隐式的Intent
- 第一步与创建显示的相同
- 创建第二个活动,在AndroidManifest中注册
<activity android:name=".SecondActivity" > <intent-filter> <action android:name="com.example.activity.ACTION_START"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter></activity>
只有action和category都匹配时,这个活动才能相应intent,因为上面设为了默认所以没有传入category也可启动intent。
如果在Intent下的一行,加入了intent.addcategory(“com.example.activitytest.MY_CATEGORY”);则在AndroidManifest中也必须添加一条
<category android:name="android.intent.category.MY_CATEGORY"/>
3.活动的启动
Intent intent=new Intent("com.example.activitytest.ACTION_START");startActivity(intent);
高级用途,可以启动其他活动
[额外可查看][http://www.lc365.net/blog/article.php?type=blog&itemid=31665]
//查看api中的intent可以查看到VIEW的相关信息Intent intent=new Intent(Intent.ACTION_VIEW);//setData传递数据intent.setData(Uri.parse("http://www.baidu.com"));startActivity(intent);当在<intent-filter>下添加<data android:scheme="http"/>scheme也可以是host,port,path,mimeType(用于指定可以处理的数据类型,允许使用通配符的方式进行指定)"http://www.baidu.com"也可以更换为"tel:10086"这样也会响应上面的事件
向下一个活动传递数据
第一个活动中用Intent 传递
String data="hello,second";Intent intent=new Intent(FirstActivity.this,SecondActivity.class);//extra_data相当于键,data为值的部分(也就是要传递的东西)intent.putExtra("extra_data",data);startActivity(intent);
第二个活动的接收
Intent intent=getIntent();//通过键得到要传递的数据String data1=intent.getStringExtra("extra_data");Log.d("SecondActivity",data1);
返回数据给上一个对象
- 要得到数据的活动内,
Intent intent = new Intent(FirstActivity.this,SecondActivity.class); //1是请求码 startActivityForResult(intent,1);
2.要发送数据的活动内
//另外可以设置全局变量Intent intent;intent=getIntent();//获得当前调用此类的intent对象。 Intent intent = new Intent(); intent.putExtra("data_return","Hello"); //RESULT_OK是结果返回码 setResult(RESULT_OK,intent); finish();
3.然后再第一个活动的onActivityResult方法中添加switch语句
switch(requestCode){ case1: if(resultCode == RESULT_OK){ String returnedData = data.getStringExtra("data_return"); Log.d("firstActivity",returnedData); } break; default: }
^通过back键返回上一个活动
则重写onBackPressed方法,里面添加
Intent intent = new Intent(); intent.putExtra("data_return","Hello"); setResult(RESULT_OK,intent); //结束当前的活动 finish();
Android 四大组件
Activiity Service ContentProvider BroadcastReceiver
intent用于android中通讯的工具,
示例
添加权限 <uses-permission android:name="android.permission.CALL_PHONE"/> "********************************************************" public class IntentActivity extends Activity implements View.OnClickListener { Button button1; Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.intent_layout); button1= (Button) findViewById(R.id.button1); button2= (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent=new Intent(); switch (v.getId()){ case R.id.button1: intent.setAction(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); break; case R.id.button2: intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); break; default: break; } }}"***********************布局文件***************************"<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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dial_button"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="call_button"/></LinearLayout>//打开网页时Intent intent = new Intent(Intent.ACTION_VIEW);intent.sentData(Uri.parse("http://www.baidu.com");startActivity(intent);
0 0
- Intent(8.19)
- Intent决议(Intent Resolution)
- Intents and Intent Filters(Intent和Intent过滤器)
- Intent的应用(初识intent)
- Intent与Intent Filter(下)
- Intent Flag介绍 intent.addFlags()
- Intent Flag介绍 intent.addFlags()
- Intent的应用(初识intent)
- Intent Flag介绍 intent.addFlags()
- Intent Flag介绍 intent.addFlags()
- Intent Flag介绍 intent.addFlags()
- Intent Flag介绍 intent.addFlags()
- intent意图过滤器(intent-filter)
- Intent Flag介绍 intent.addFlags()
- startActivityForRequest(Intent , int)
- intent (receiver)
- android -- >intent (信使)
- (android developers)intent
- 自定义UITableViewCell勾选框的保持
- MySQL创建表结构里面的那个长度对于整型的作用
- oracle实现查询每一个部门的员工工资排在前三的员工的基本信息详细举例
- CAS:Compare And Swap 无锁
- eclipse 搭建activiti5.17项目生成流程过程
- Intent(8.19)
- 九度OJ 题目1065:输出梯形
- POJ -3414-Pots
- 人体所需的7大营养素
- 自己平时长期积累的java资料可供大家学习
- 数据库读写分离和垂直分库、水平分表
- Windows环境下教你用Eclipse ADT 插件生成.h/.so文件,Java下调用JNI,轻松学习JNI
- Mac下安装from matplotlib import pyplot或者 import matplotlib.pyplot as plt报错
- UVA442矩阵乘法之求出多个矩阵相乘乘法的次数