Intent加强
Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来请求。在这些组件之间的通讯中,主要是由Intent协助完成的。Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class Main extends Activity { private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 加载布局文件
button = (Button) this.findViewById(R.id.button);//
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
// 在意图中传递数据
intent.putExtra("name", "张三");
intent.putExtra("age", 23);
intent.putExtra("address", "北京"); intent.setClass(Main.this, OtherActivity.class); // 启动意图
startActivity(intent);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试使用Intent传递数据"></Button> </LinearLayout>
OtherActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity { private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg); Intent intent = getIntent();
int age = intent.getIntExtra("age", 0);
String name = intent.getStringExtra("name");
String address = intent.getStringExtra("address"); textView.setText("age--->>" + age + "\n" + "name-->>" + name + "\n"
+ "address->>" + address);
}
}
<?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:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView> </LinearLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.laoluo.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.laoluo.test.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>
<activity
android:name="com.laoluo.test.OtherActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class Main extends Activity {
/** Called when the activity is first created. */
private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 声明一个意图
Intent intent = new Intent();
intent.setClass(Main.this, OtherActivity.class);
//在OtherActivity中先声明需要的静态变量,然后将值赋给OtherActivity中的静态变量
OtherActivity.age = 23;
OtherActivity.name = "jack";
startActivity(intent);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用静态变量传递数据" />
</LinearLayout>
package com.laoluo.test; import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity { private TextView textView;
public static String name;
public static int age;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other); textView = (TextView)this.findViewById(R.id.msg); textView.setText("-name->>"+name+"\n"+"-age-->>"+age);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.laoluo.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.laoluo.test.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>
<activity
android:name="com.laoluo.test.OtherActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Button; public class Main extends Activity { private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // 从android系统中调用剪切板的服务
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String name = "jack";
clipboardManager.setText(name);//将数据设置到剪切板中 Intent intent = new Intent(Main.this, OtherActivity.class);
startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <Button
android:text="使用剪切板传递数据"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.widget.TextView; public class OtherActivity extends Activity { private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other); textView = (TextView) this.findViewById(R.id.msg); ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString();//取出剪切板中的内容
textView.setText(msgString); }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.laoluo.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.laoluo.test.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>
<activity
android:name="com.laoluo.test.OtherActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>
import java.io.Serializable;
public class MyData implements Serializable {
//这里最好给该对象设置一个id:private static final long serialVersionUID = 1L;
private String name;
private int age;
public MyData(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "MyData [name=" + name + ", age=" + age + "]";
}
}
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Base64;
import android.view.View;
import android.widget.Button; public class Main extends Activity { private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { MyData myData = new MyData("jack", 23);
//将对象转换成字符串
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String base64String = "";
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(myData);
base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
objectOutputStream.close();
} catch (Exception e) { }
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(base64String);
Intent intent =new Intent(Main.this,OtherActivity.class);
startActivity(intent);
}
});
}
}
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Base64;
import android.widget.TextView; public class OtherActivity extends Activity { private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other); textView = (TextView) this.findViewById(R.id.msg); ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString();//取出剪切板中的内容 byte[] base64_byte = Base64.decode(msgString, Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
MyData myData = (MyData) objectInputStream.readObject();
textView.setText(myData.toString());
} catch (Exception e) { } }
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class Main extends Activity {
private Button button;
private MyApp myApp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myApp = (MyApp)getApplication();
myApp.setName("jack");//修改之后的名称 Intent intent = new Intent(Main.this,OtherActivity.class);
startActivity(intent);
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity { private MyApp myApp;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other); textView = (TextView)this.findViewById(R.id.msg); myApp = (MyApp)getApplication(); textView.setText("-appname-->>"+myApp.getName());
}
}
import android.app.Application;
public class MyApp extends Application {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void onCreate() {
super.onCreate();
setName("张三");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.laoluo.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.laoluo.test.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>
<activity
android:name="com.laoluo.test.OtherActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class Main extends Activity { private Button button;
private final static int REQUESTCODE = 1; //返回的结果码
private EditText one,two,result; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); button = (Button) this.findViewById(R.id.button);
one = (EditText)this.findViewById(R.id.one);
two = (EditText)this.findViewById(R.id.two);
result = (EditText)this.findViewById(R.id.result); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString()); Intent intent = new Intent(Main.this, OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b); //启动Intent
startActivityForResult(intent, REQUESTCODE);//启动意图的另一种方法,这种方法可以返回结果,
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2){
if(requestCode==REQUESTCODE){
int three = data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/one"
android:layout_width="80dp"
android:layout_height="wrap_content">
</EditText> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" + ">
</TextView>
<EditText
android:id="@+id/two"
android:layout_width="80dp"
android:layout_height="wrap_content">
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = ">
</TextView>
<EditText
android:id="@+id/result"
android:layout_width="80dp"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算结果">
</Button> </LinearLayout>
OtherActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class OtherActivity extends Activity { private Button button;
private TextView textView;
private EditText editText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other); button = (Button)this.findViewById(R.id.button2);
textView = (TextView)this.findViewById(R.id.msg);
editText = (EditText)this.findViewById(R.id.three); Intent intent = getIntent(); int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0); textView.setText(a+" + "+b+" = "+" ? "); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { Intent intent = new Intent();
int three = Integer.parseInt(editText.getText().toString()); intent.putExtra("three", three); //通过Intent对象返回结果,setResult方法,
setResult(2, intent);
finish();//结束当前的Activity的生命周期。
}
});
}
}
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView> <EditText
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果">
</Button> </LinearLayout>
Intent加强的更多相关文章
- Parcelable序列化的使用,关于intent.getParcelableArrayExtra的使用
Parcelable相较于Serializable的效率更高 public class ChargeMode implements Parcelable{ public String name; pu ...
- android Intent介绍
Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...
- 很不错的Intent用法 适用于正在开发的伙伴。自己看到了,也分享给大家吧。
本文介绍Android中Intent的各种常见作用. 1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开 ...
- 在Android中Intent的概念及应用(二)——Intent过滤器相关选项
一.如果多个Activity拥有同一个Intent Action,启动时用同一个Action启动会是什么情况? 如何指定某一个Activity启动? 在多个Activity拥有同一个Intent Ac ...
- 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent
Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...
- Android的Intent.FLAG_ACTIVITY_CLEAR_TOP无效
转载:http://blog.csdn.net/u011361576/article/details/48626237 今天写代码遇到了一个问题: 当 B - A - B 跳转的时候,使用Intent ...
- Intent
1.http://blog.csdn.net/daogepiqian/article/details/50606474 2.http://blog.csdn.net/wulianghuan/artic ...
- Android基础学习第三篇—Intent的用法
写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...
- Android 笔记 Intent and Bundle day7
学习了Intent与Bundle的使用,进行应用中的交互 package com.example.intent; import android.app.Activity; import android ...
- Android Intent Flags
Intent,顾名思义,即意图,通常用它来启动一个应用程序组件( Activity, Service, 或者 BroadCast Receiver ).在启动一个新的 Activity 时,可以通过给 ...
随机推荐
- 如何做自己的服务监控?spring boot 2.x服务监控揭秘
Actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...
- 高并发连接导致打开文件过多:java.io.IOException: Too many open files 解决方法
用 CentOS 做 API 接口服务器供其他终端调用时,并发量高会报错:java.io.IOException: Too many open files. 其原因是在 Linux 下默认的Socke ...
- [Luogu 3787] 冰精冻西瓜
Description 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有根树,琪露诺想要把它们冷冻起来慢慢吃. 这些西瓜蔓具有神奇的性质, ...
- MyBatis从入门到放弃二:传参
前言 我们在mapper.xml写sql,如果都是一个参数,则直接配置parameterType,那实际业务开发过程中多个参数如何处理呢? 从MyBatis API中发现selectOne和selec ...
- c# 二进制序列化
public static T Deserialize<T, S>(S stream) where S : Stream where T : class, new() { using (s ...
- elasticsearch6.7 05. Document APIs(10)Reindex API
9.REINDEX API Reindex要求为源索引中的所有文档启用_source. reindex 不会配置目标索引,不会复制源索引的设置.你需要在reindex之前先指定mapping,分片数量 ...
- java_分数
题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这个类要 ...
- Maven + SSM + Kaptcha 实现用户登录时验证码的获取(问题:302 Found)
pom.xml(对Kaptcha.jar的引用) <!-- 验证码 jar kaptcha--> <dependency> <groupId>com.github. ...
- 解决Linux服务器tomact-8.0启动慢的问题
环境信息: CentOS release 6.8 tomcat-8.0 JDK1.8 一.启动tomcat #sh /root/tomcat-8.0/bin/startup.sh #tailf /ro ...
- 初学CSS-2-文本的属性
文本装饰属性: 格式:text-decoration:underline: 取值:underline(下划线) line-through(删除线) overline(上划线) none(什么都没有) ...