android入门之intent示例
来源:互联网 发布:网络用语弊端 英语ppt 编辑:程序博客网 时间:2024/05/21 17:12
从一个界面跳转到另一个界面,把一个界面的信息传送给另一个界面,就需要用到我们的intent方法,接下来带领大家做一个小示例。
配置信息
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zlg.text2"> <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=".Activity_toast_image" /> <activity android:name=".FrameActivity" /> <activity android:name=".myviewActivity" /> <activity android:name=".LifeActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <data android:scheme="http" /> </intent-filter> </activity> <activity android:name=".IntentActivity"> <intent-filter> <action android:name="zlgapp" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="qlu.com" android:path="/android" android:scheme="ftp" /> </intent-filter> </activity> <activity android:name=".subActivity" /> <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="broadcast.com"></action> </intent-filter> </receiver> </application></manifest>
首先创建一个MainActivity,还覆盖了他的所有方法,以观察他的生命周期,携带信息。
package com.example.zlg.text2;//快捷键ctrl+o覆盖类import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;public class LifeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_life); Log.d(LifeActivity.class.toString(),"onCreate"); Button button=(Button)findViewById(R.id.life_button); } @Override protected void onStart() { super.onStart(); Log.d(LifeActivity.class.toString(),"onStart"); } @Override protected void onResume() { super.onResume(); Log.d(LifeActivity.class.toString(),"onResume"); } @Override protected void onRestart() { super.onRestart(); Log.d(LifeActivity.class.toString(),"onRestart"); } @Override protected void onPause() { super.onPause(); Log.d(LifeActivity.class.toString(),"onPause"); } @Override protected void onStop() { super.onStop(); Log.d(LifeActivity.class.toString(),"onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(LifeActivity.class.toString(),"onDestroy"); } public void switchAct(View v) { Intent intent=new Intent(this,IntentActivity.class); intent.putExtra("key","你好"); startActivity(intent); } public void impStart(View v) { Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); startActivity(intent); } public void impStart2(View v) { Intent intent=new Intent("zlgapp", Uri.parse("ftp://qlu.com/android")); startActivity(intent); } public void sendBroadcast(View v) { Intent intent=new Intent("broadcast.com"); intent.putExtra("msg","message"); sendBroadcast(intent); } public void startSub(View v) { Intent intent=new Intent(this,subActivity.class); startActivityForResult(intent,1); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==1) { if(resultCode==RESULT_OK){ Toast.makeText(this, "数据为"+data.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "已经取消", Toast.LENGTH_SHORT).show(); } } }}//onCreate onStart onResume打开Activity//onPause onStop 切换到别的程序// onRestart onStart onResume//切换回来//onPause onStop onDestroy//退出程序
创建一个layout_main.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"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显式跳传跳转到intentActivity" android:id="@+id/life_button" android:onClick="switchAct"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/life_button2" android:onClick="impStart" android:text="隐式跳传跳转到百度首页"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/life_button3" android:onClick="impStart2" android:text="隐式跳传跳转过滤intentActivity"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取subActivity的返回值跳转到lifeActivity" android:onClick="startSub" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播BroadcastReceiver" android:onClick="sendBroadcast" /></LinearLayout>
跳转到的IntentActivity,接收信息。
package com.example.zlg.text2;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;public class IntentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_intent); Intent intent=getIntent(); String str=intent.getStringExtra("key"); TextView textView=(TextView)findViewById(R.id.intent_tv); textView.setText(str); }}
其layout_intent.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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/intent_tv"/></LinearLayout>
然后创建一个subActivity
package com.example.zlg.text2;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class subActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_sub); Button button1=(Button)findViewById(R.id.but1); Button button2=(Button)findViewById(R.id.but2); } public void OK(View v) { String str=((EditText)findViewById(R.id.ed)).getText().toString(); Intent result = new Intent(); result.putExtra("msg",str); setResult(RESULT_OK, result); finish(); } public void NO(View v) { finish(); }}
其layout_sub.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"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ed"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/but1" android:onClick="OK" android:text="确认"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/but2" android:text="取消" android:onClick="NO"/></LinearLayout>
在创建一个 MyReceiver 的广播
package com.example.zlg.text2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Toast.makeText(context, "广播已发送,信息为:"+intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); }}
[资源下载地址](http://download.csdn.net/download/u011637078/10050880)
有了这个小示例,我们就可以借此发挥我们的想象了!
阅读全文
0 0
- android入门之intent示例
- Android入门之Intent
- Android入门开发之Intent
- Android 入门之Intent应用
- android之Intent几个基础用法示例
- android之Intent几个基础用法示例
- Android学习之路---Intent应用示例
- Android开发入门之Intent深入解剖
- android入门开发之Intent学习
- android ---Intent示例大全
- android intent使用示例
- Android入门:Intent入门
- Android开发之 常用的Intent的URI及示例
- android教程之intent的action属性使用示例(intent发短信)
- Android入门:隐式Intent
- Android入门/Intent(六)
- android 初级入门 intent
- Android入门:隐式Intent
- Linux系统命令及Shell脚本实践指南笔记
- poj 1017装箱问题(贪心)
- 修改MySQL数据库中表和表中字段的编码方式的方法
- 小试牛刀-递归下降算法(2)
- 【DP】BZOJ1084(SCOI2005)[最大子矩阵]题解
- android入门之intent示例
- tar命令
- Codeforces Round #444 (Div. 2) C. Solution for Cube
- Linux学习笔记-实体链接与符号链接
- DOS的基础命令
- Bigtable Merge-Dump存储引擎
- java中static关键字
- mysql 不能远程连接
- 逆波兰表达式及atof函数的调用