Intent用法总结

来源:互联网 发布:拜乐生物杀蟑饵剂 知乎 编辑:程序博客网 时间:2024/04/18 17:23

 

activities,services, broadcast receivers均是由intent开启的.

但是这三种用法不会重叠。

在帮助文档中是这样介绍的:

·        An Intentobject is passed to Context.startActivity() orActivity.startActivityForResult() to launch an activity or get an existing activity to dosomething new. (It can also be passed to Activity.setResult() to return information to the activity that called startActivityForResult()

 

·        An Intentobject is passed to Context.startService() to initiate a service or deliver new instructions to anongoing service. Similarly, an intent can be passed to Context.bindService() to establish a connection between the calling componentand a target service. It can optionally initiate the service if it's notalready running.

·        Intentobjects passed to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), orContext.sendStickyBroadcast()) are delivered to all interested broadcast receivers.Many kinds of broadcasts originate in system code.

第三种用法,传递给broadcast method,例如Context.sendBroadcast(), Context.sendOrderedBroadcast(), orContext.sendStickyBroadcast()

Android系统代码中有很多这种用法,我用Google代码搜索随意找了下,整理如下:

 

我自己找的,不要鄙视,有观点错误还请各位指教:

定义:

frameworks/base.gitcorejavaandroidcontent›Intent.java

/**
     * Broadcast Action: The time was set.
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_TIME_CHANGED = "android.intent.action.TIME_SET";

发送出去:

packages/apps/Settings.gitsrccomandroidsettings›DateTimeSettings.java

private void timeUpdated() {
        Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
        sendBroadcast(timeChanged);
    }

接收:

packages/apps/Settings.gitsrccomandroidsettings›DateTimeSettings.java

IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
            mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);

 

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateTimeAndDateDisplay();
        }
    };

mIntentReceiverregisterunregister

register

packages/apps/Settings.gitsrccomandroidsettings›DateTimeSettings.java onResume()中

IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(mIntentReceiver, filter, null, null);

unregister

packages/apps/Settings.gitsrccomandroidsettings›DateTimeSettings.java onResume()中

protected void onPause() {
        super.onPause();
        unregisterReceiver(mIntentReceiver);
        。。。。
    }

通常情况下BroadcastReceiver都是在onResume时被register,在onStop时被unregister(我自己认为的,呵呵)

当然BroadcastReceiver也可以自己独立不写在Activity里面,这时就会在AndroidManifest.xml里面有定义类似

/development.gitsamplesApiDemos›AndroidManifest.xml

<receiver android:name=".appwidget.ExampleBroadcastReceiver" android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
                <action android:name="android.intent.ACTION_TIME" />
            </intent-filter>
        </receiver>
其实上面写在activity的方式也需要在xml里面加对应的intetfilter,但是上面那段代码动态的写在activity里面了,所以xml里面就没有写

接收:

development.gitsamplesApiDemossrccomexampleandroidapisappwidget›ExampleBroadcastReceiver.java

public class ExampleBroadcastReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
       …………
}

 

另外我自己有写了一些关于intent用于service的一些用法的例子(没力气贴代码了,感兴趣的下工程看),包括intent用于broadcast的一些用法,将它们都写在一个工程里面了,方便查阅,基本文档中提到的用法我都有试。Intent用于activity的方法比较常见,所以我就没有写了。有兴趣的可以下载整个工程看,我是小小菜鸟请勿鄙视。

地址:http://download.csdn.net/source/2296676

 

原创粉丝点击