Intent



src 中的 MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.btn_start_second_by_action).setOnClickListener(this);
		findViewById(R.id.btn_call).setOnClickListener(this);
		findViewById(R.id.btn_dial).setOnClickListener(this);
		findViewById(R.id.btn_start_second).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_start_second:
			// 激活Activity,最简单的语法
			Intent intent = new Intent(MainActivity.this, SecondActivity.class);

			intent.putExtra("name", "Mike");
			intent.putExtra("age", 10);
			startActivity(intent);
			break;

		case R.id.btn_dial:
			Intent it = new Intent();
			it.setAction("android.intent.action.DIAL");
			it.setData(Uri.parse("tel:10086"));
			startActivity(it);
			break;
			
		case R.id.btn_call:
			Intent it2 = new Intent();
			it2.setAction(Intent.ACTION_CALL);
			it2.setData(Uri.parse("tel:10086"));
			startActivity(it2);
			break;
			
		case R.id.btn_start_second_by_action:
			Intent it3 = new Intent();
			it3.setAction("number_two");
			startActivity(it3);
			break;
		}
		
	}

}

src 中的 SecondActivity:

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		Intent intent = getIntent();
		String n = intent.getStringExtra("name");
		int a = intent.getIntExtra("age", 5454689);
		
		Log.d("", "n=" + n + ", a=" + a);
		Toast.makeText(this, "n=" + n + ", a=" + a, Toast.LENGTH_LONG).show();
	}

}
新建一个 android.os 的包

CustomBundle:

public class CustomBundle {

	private HashMap<String, Object> map = new HashMap<String, Object>();

	public void put(String key, int value) {
		map.put(key, value);
	}

	public int getInt(String key) {
		return Integer.valueOf(map.get(key).toString());
	}

	public long getLong(String key) {
		return Long.valueOf(map.get(key).toString());
	}

	public void put(String key, long value) {
		map.put(key, value);
	}

	public void put(String key, String value) {
		map.put(key, value);
	}
}
res 中的 activity_main.xml :

<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=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="1"
        android:textSize="50sp" />

    <Button
        android:id="@+id/btn_start_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="激活第2个Activity" />

    <Button
        android:id="@+id/btn_dial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_start_second"
        android:layout_centerHorizontal="true"
        android:text="拨打10086" />
    <Button
        android:id="@+id/btn_call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_dial"
        android:layout_centerHorizontal="true"
        android:text="呼叫10086" />
    <Button
        android:id="@+id/btn_start_second_by_action"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_call"
        android:layout_centerHorizontal="true"
        android:text="隐式激活SecondActivity" />

</RelativeLayout>
res 中的 activity_second.xml:

<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=".SecondActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="2"
        android:textSize="50sp" />

</RelativeLayout>
文件清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.tedu.intent"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-permission android:name="android.permission.C"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.tedu.intent.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="cn.tedu.intent.SecondActivity"
            android:label="@string/title_activity_second" >
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="second" />
                <action android:name="number2" />
                <action android:name="number_two" />
                <category android:name="category_second"/>
                <category android:name="category_number2"/>
                <category android:name="category_number_two"/>
            </intent-filter>
        </activity>
    </application>

</manifest>









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值