Intent可以激活Android应用的三个核心组件:活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver).
Intent中有两种意图:分别是显式意图和隐式意图
1、显示意图:明确指定了组件名的Intent.
三种方式:
1)
intent.setComponent(new Component(MainActivity.this,OtherActivity.class));
Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(data)找到最合适的组件来处理这个意图.
例如:拨打电话的意图:
Intent intent=new Intent(Intent.ACTION_CALL,uri);
首先,新建一个Android项目--->取名IntentDemo
MainActivity.java的代码如下:
private EditText name = null;
private Button submit = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText)findViewById(R.id.etname);
submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = name.getText().toString();
Intent intent = new Intent();
intent.putExtra("name", content);
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name"
android:id="@+id/name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etname"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:id="@+id/submit"
/>
</LinearLayout>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">Intent应用</string>
<string name="name">你最亲爱的人 :</string>
<string name="submit">提交无返</string>
<string name="resubmit">提交有返</string>
<string name="resend">返回</string>
</resources>
private TextView result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.other);
result = (TextView)findViewById(R.id.result);
Intent intent = this.getIntent();
String content = intent.getExtras().getString("name");
result.setText(content);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
/>
</LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gem.teset"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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=".OtherActivity"
android:label="@string/app_name"
/>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
![](../p_w_upload/201110/155948477.png)
![](../p_w_upload/201110/160028287.png)
首先,新建一个Android项目--->取名IntentPhoneUrl
MainActivity.java的代码如下:
private EditText num = null;
private Button call = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
num = (EditText)findViewById(R.id.num);
call =(Button)findViewById(R.id.call);
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNum = num.getText().toString();
Uri uri = Uri.parse("tel:" + phoneNum);
Intent intent = new Intent(Intent.ACTION_CALL,uri);
startActivity(intent);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/num"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/call"
android:text="拨号"
/>
</LinearLayout>
<resources>
<string name="hello">电话号码</string>
<string name="app_name">IntentPhoneUrl</string>
</resources>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gem.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
</application>
<uses-sdk android:minSdkVersion="8" />
<!-- 拨号权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- 发送信息权限 -->
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
效果图如下:
![](../p_w_upload/201110/160117560.png)