River的Android学习笔记——Intent意图

一.Android中的“邮递员”——Intent

      Intent字面上理解就是“意图”的意思,实际上他确实承载了Android的各个activity的“意图”,所谓activity的“意图”就是指当前activity是想向其他activity传递数据,还是想向系统或者其他应用发送指令和请求等等。简单的说,Android用户都知道,在使用应用的时候不可能只使用一个“窗口”(^_^:占时说是窗口,其实这里的activity思想和windows中的“窗口”思想有很大相同之处,但稍有区别,以后再说),而是和应用中的其他窗口进行交互,或者和其他应用进行交互(例如:通过某应用打开浏览器。),这时就需要一个“邮递员”进行传送信息了,而它的名字叫Intent。

    Intent类在 android.content.Intent 这儿。

二.所涉及文件(以下所讨论均针对本小节所提供源代码)

    这里我创建了两个activity,分别是FirstActivity和SecondActivity。对应的文件分别是FirstActivity.java SecondActivity.ava。布局文件分别是first_layout.xml和second_layout.xml。

    先上布局:

    first_layout.xml

   

<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.river.ch1demo.FirstActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1" /><!-- 注意:text值最好写进string.xml文件中 -->

</RelativeLayout>
</span>

    second_layout.xml

<span style="font-size:18px;"><?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:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        />><!-- 注意:text值最好写进string.xml文件中 -->
</LinearLayout>
</span>

    再上Java:

    FirstActivity.java

<span style="font-size:18px;">import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends Activity {
	private Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);
		btn = (Button)findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				/*
				 * 显式Itent;
				 */
				/*Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
				startActivity(intent);*/
				
				
				/*
				 * 隐式Intent
				 */
				/*Intent intent = new Intent("com.river.ch1demo.ACTION_START");
				startActivity(intent);*/
				
				/*
				 * 隐式的更多用法(打开浏览器)
				 */
				//方法一:
				/*Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com") );
				startActivity(intent);*/
				//方法二:
				/*Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse("http://www.baidu.com"));
				//Intent intent = new Intent("android.intent.action.VIEW");
				startActivity(intent);*/
				/*
				 * 返回一个数据给上一个活动
				 */
				Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
				startActivityForResult(intent, 1);//请求码
				
			}
			
			
		});
	}
	/*
	 * (非 Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO 自动生成的方法存根
		//super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			if (resultCode == RESULT_OK) {
				//Intent intent = new Intent();
				String returnedData = data.getStringExtra("data_return");
				MessageBox(returnedData);
			}
			
			break;

		default:
			//break;
		}
	}
	/*
	 * 类似于Windows下的MessageBox函数,用到了Toast类(吐司)
	 */
	public void MessageBox(String text){
		Toast.makeText(FirstActivity.this, text, Toast.LENGTH_LONG).show();
	}
}
</span>

    SecondActivity.java

<span style="font-size:18px;">import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class SecondActivity extends Activity{
	private Button button2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.second_layout);
		button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				Intent intent = new Intent();
				intent.putExtra("data_return", "Hello River!");
				setResult(RESULT_OK,intent);
				finish();
			}
		});
	}
}
</span>

    AndroidManifest.xml(重要文件)

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.river.ch1demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

    <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">
            <intent-filter >
                <!-- <action android:name="com.river.ch1demo.ACTION_START"/> -->
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <!-- <data android:scheme="http"/> -->
            </intent-filter>
        </activity>
    </application>

</manifest>
</span>


三. 显式Intent

Intent有多个构造函数重载,显式Intent用到的是Intent(Context packageContext,Class<?>cls)这个构造函数。顾名思义,显式就是能看的着的,也就是说这个“意图”极其明显:

<span style="font-size:18px;">     btn.setOnClickListener(new OnClickListener(){

	@Override
	    public void onClick(View arg0) {
	    // TODO 自动生成的方法存根
	     /*
		* 显式Itent;
		*/
		  Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
			startActivity(intent);				
	    }			
	});
	}</span>


 



 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值