Intent

来源:互联网 发布:南京软件 外包公司 编辑:程序博客网 时间:2024/04/30 08:17



src 中的 MainActivity:

public class MainActivity extends Activity implements View.OnClickListener {@Overrideprotected 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);}@Overridepublic 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 {@Overrideprotected 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
原创粉丝点击