Intent
在Android中intent是消息的载体,使用它来让组件(component)执行操作。其主要用途有三:
1.启动一个Activity
2.启动一个Service
3.派送broadcast
Intent有两种类型:
显式的Intent(Explicit intents) 隐式的Intent(Implicit intents)
在这里我要说明的是隐式的Intent启动Activity
MainActivity.java
<span style="color:#339999;">package xl.intent_filter.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
}
void initControl()
{
Button btn = (Button)findViewById(R.id.button1);
if(null != btn )
{
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setType(getResources().getString(R.string.image_all));
startActivity(intent);
}
});
}
Button btnbroad = (Button)findViewById(R.id.button2);
if(null != btnbroad){
btnbroad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("action.send.xl.broadcast");
sendBroadcast(intent);
}
});
}
}
}</span><span style="color:#ffcc66;">
</span>
AndroidManifest.xml
<span style="background-color: rgb(255, 255, 255);"><span style="color:#3333ff;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xl.intent_filter.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" android:name="XLApplication">
<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="ImplicitAcitivity">
<intent-filter>
</span><span style="color:#ff0000;"><category android:name="android.intent.category.DEFAULT" /></span><span style="color:#3333ff;">
<action android:name="android.intent.action.VIEW"/>
<data android:mimeType="@string/image_all"/>
</intent-filter>
</activity>
<receiver android:name="XlBroadcastReceiver">
<intent-filter>
<action android:name="action.send.xl.broadcast"/>
</intent-filter>
</receiver>
</application>
</manifest></span></span><span style="color: rgb(255, 255, 255); background-color: rgb(51, 51, 255);">
</span>
Note: Android automatically applies the the
CATEGORY_DEFAULT
category to all implicit intents passed tostartActivity()
and startActivityForResult()
. So if you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT"
in its intent filters (as shown in the previous<intent-filter>
example.
在这里还要说明关于Intent-Filter的事情,如果在某一个Activity里就必须在AndroidManifest.xml文件中声明,
<activity android:name="ImplicitAcitivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW"/>
<data android:mimeType="@string/image_all"/>
</intent-filter>
</activity>
如果是针对BroadcastReceiver的话可以在代码中通过registerReceiver来注册,这样做的目的是为了当程序运行的时候才去监听某些事件。
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
registerReceiver(mModel, filter);