Intent:提供了一种通用的消息系统,它允许在你的应用程序与其他的应用程序间传递Intent来执行动作和产生事件.相当于一个请求.
Intent可以激活Android应用的三个核心组件:活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver).
Intent中有两种意图:分别是显式意图和隐式意图
1、显示意图:明确指定了组件名的Intent.
三种方式:
1)
Intent intent=new Intent();
intent.setComponent(new Component(MainActivity.this,OtherActivity.class));
2)                                                        
intent.setClass(MainActivity.this,OtherActivity.class);
3)
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
2、隐式意图:没有明确指定组件名的Intent.
Android系统会根据隐式意图中设置的动作(action)、类别(category)、数据(data)找到最合适的组件来处理这个意图.
例如:拨打电话的意图:
Uri uri=Uri.parse("tel:"+phoneCode);
Intent intent=new Intent(Intent.ACTION_CALL,uri);
下面就两种意图分别举例:
首先,新建一个Android项目--->取名IntentDemo
MainActivity.java的代码如下:
public class MainActivity extends Activity {
        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);
                      }
               });
        }
}
Main.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<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>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
OtherActivity.java的代码如下:
public class OtherActivity extends Activity {
        
  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);
    
  }
}
other.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<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>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
效果图如下:
 


首先,新建一个Android项目--->取名IntentPhoneUrl
MainActivity.java的代码如下:
public class MainActivity extends Activity {
  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);

      }
    });
  }
}
main.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<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>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
        <string name="hello">电话号码</string>
        <string name="app_name">IntentPhoneUrl</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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>

效果图如下: