精华内容
下载资源
问答
  • Android Broadcast

    2015-12-17 09:08:33
    Android Broadcast

    Android Broadcast 



    1:广播接收者,四大组件之一,
    常见的广播:
    检查到WiFi的时候,系统通知发现WwiFi是否加入
    当收到短信的时候,系统发送短信收到的信息,
    当手机收到电话的时候,系统发送广播,告诉应用,来电话,
    当手机重新开机之后,系统会发送广播,告诉所有的应用,手机启动了。
    短信的拦截。

    展开全文
  • Android BroadCast

    千次阅读 2010-05-19 09:39:00
    1. 自定义自已的Broadcast receiver: public class myReceiver extends BroadcastReceiver{ @Override public void onReceive (Context context, Intent intent) { if (intent.getAction().equa

    1. 自定义自已的Broadcast receiver:

    public class myReceiver extends BroadcastReceiver{
        @Override
        public void onReceive (Context context, Intent intent) {
            if (intent.getAction().equals("com.spreadcomm.myparceltest.test")){
                Toast.makeText(context, "This is from brodcast!", Toast.LENGTH_SHORT).show();
            }
            else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
                Toast.makeText(context, "Charing up!", Toast.LENGTH_LONG).show();
            }   
        }
    }

    2. 注册Receiver:

         a. 通过context:

             registerReceiver (BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)

         b. 通过Manifest.xml:

    <activity android:name=".myParceTest"  android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".myReceiver">   
         <intent-filter>   
             <action android:name="com.myparceltest.test" />   
         </intent-filter>   
    </receiver>

    3. 发送Broadcast:

     

    4. Anroid 提供的Brodcast Action

    ACTION_TIME_TICK 每分钟发送,表明时间在运行
    ACTION_TIME_CHANGED 当用户更改了设备上的时间时发送
    ACTION_TIMEZONE_CHANGED 当用更改了设备上的时区时发送
    ACTION_BOOT_COMPLETED 当设备启动完成了以后
    ACTION_PACKAGE_ADDED 当添加了新的包到设备上时发送
    ACTION_PACKAGE_REMOVED 当删除一个包时发送
    ACTION_PACKAGE_CHANGED 当包被更改时,如disable 改为
    ACTION_PACKAGE_RESTARTED 当程序被中断,重新打开的时候
    ACTION_UID_REMOVED 一个UID从系统删除的时候
    ACTION_SHUTDOWN 关机的时候
    ACTION_POWER_DISCONNECTED 断开连接电源
    ACTION_POWER_CONNECTED 连接外部电源时
    ACTION_BATTERY_CHANGED 当电池的状态,充电改变时发送
    ACTION_PACKAGE_DATA_CLEARED The user has cleared the data of a package. This should be preceded by ACTION_PACKAGE_RESTARTED, after which all of its persistent data is erased and this broadcast sent. Note that the cleared package does not receive this broadcast.

     

    Api Level 3:(SDK 1.5)

    Xml代码 复制代码

    1. android.bluetooth.a2dp.intent.action.SINK_STATE_CHANGED  
    2. android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED  
    3. android.bluetooth.intent.action.BOND_STATE_CHANGED_ACTION  
    4. android.bluetooth.intent.action.DISCOVERY_COMPLETED  
    5. android.bluetooth.intent.action.DISCOVERY_STARTED  
    6. android.bluetooth.intent.action.HEADSET_ADUIO_STATE_CHANGED  
    7. android.bluetooth.intent.action.HEADSET_STATE_CHANGED  
    8. android.bluetooth.intent.action.NAME_CHANGED  
    9. android.bluetooth.intent.action.PAIRING_CANCEL  
    10. android.bluetooth.intent.action.PAIRING_REQUEST  
    11. android.bluetooth.intent.action.REMOTE_DEVICE_CONNECTED  
    12. android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED  
    13. android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED  
    14. android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECTED  
    15. android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECT_REQUESTED  
    16. android.bluetooth.intent.action.REMOTE_DEVICE_FOUND  
    17. android.bluetooth.intent.action.REMOTE_NAME_FAILED  
    18. android.bluetooth.intent.action.REMOTE_NAME_UPDATED  
    19. android.bluetooth.intent.action.SCAN_MODE_CHANGED  
    20. android.intent.action.AIRPLANE_MODE  
    21. android.intent.action.BATTERY_CHANGED  
    22. android.intent.action.BATTERY_LOW  
    23. android.intent.action.BOOT_COMPLETED  
    24. android.intent.action.CAMERA_BUTTON  
    25. android.intent.action.CONFIGURATION_CHANGED  
    26. android.intent.action.DATA_SMS_RECEIVED  
    27. android.intent.action.DATE_CHANGED  
    28. android.intent.action.DEVICE_STORAGE_LOW  
    29. android.intent.action.DEVICE_STORAGE_OK  
    30. android.intent.action.GTALK_CONNECTED  
    31. android.intent.action.GTALK_DISCONNECTED  
    32. android.intent.action.HEADSET_PLUG  
    33. android.intent.action.INPUT_METHOD_CHANGED  
    34. android.intent.action.MANAGE_PACKAGE_STORAGE  
    35. android.intent.action.MEDIA_BAD_REMOVAL  
    36. android.intent.action.MEDIA_BUTTON  
    37. android.intent.action.MEDIA_CHECKING  
    38. android.intent.action.MEDIA_EJECT  
    39. android.intent.action.MEDIA_MOUNTED  
    40. android.intent.action.MEDIA_NOFS  
    41. android.intent.action.MEDIA_REMOVED  
    42. android.intent.action.MEDIA_SCANNER_FINISHED  
    43. android.intent.action.MEDIA_SCANNER_SCAN_FILE  
    44. android.intent.action.MEDIA_SCANNER_STARTED  
    45. android.intent.action.MEDIA_SHARED  
    46. android.intent.action.MEDIA_UNMOUNTABLE  
    47. android.intent.action.MEDIA_UNMOUNTED  
    48. android.intent.action.NEW_OUTGOING_CALL  
    49. android.intent.action.PACKAGE_ADDED  
    50. android.intent.action.PACKAGE_CHANGED  
    51. android.intent.action.PACKAGE_DATA_CLEARED  
    52. android.intent.action.PACKAGE_INSTALL  
    53. android.intent.action.PACKAGE_REMOVED  
    54. android.intent.action.PACKAGE_REPLACED  
    55. android.intent.action.PACKAGE_RESTARTED  
    56. android.intent.action.PHONE_STATE  
    57. android.intent.action.PROVIDER_CHANGED  
    58. android.intent.action.REBOOT  
    59. android.intent.action.SCREEN_OFF  
    60. android.intent.action.SCREEN_ON  
    61. android.intent.action.TIMEZONE_CHANGED  
    62. android.intent.action.TIME_SET  
    63. android.intent.action.TIME_TICK  
    64. android.intent.action.UID_REMOVED  
    65. android.intent.action.UMS_CONNECTED  
    66. android.intent.action.UMS_DISCONNECTED  
    67. android.intent.action.USER_PRESENT  
    68. android.intent.action.WALLPAPER_CHANGED  
    69. android.media.AUDIO_BECOMING_NOISY  
    70. android.media.RINGER_MODE_CHANGED  
    71. android.media.VIBRATE_SETTING_CHANGED  
    72. android.net.conn.BACKGROUND_DATA_SETTING_CHANGED  
    73. android.net.wifi.NETWORK_IDS_CHANGED  
    74. android.net.wifi.RSSI_CHANGED  
    75. android.net.wifi.SCAN_RESULTS  
    76. android.net.wifi.STATE_CHANGE  
    77. android.net.wifi.WIFI_STATE_CHANGED  
    78. android.net.wifi.supplicant.CONNECTION_CHANGE  
    79. android.net.wifi.supplicant.STATE_CHANGE  
    80. android.provider.Telephony.SIM_FULL  
    81. android.provider.Telephony.SMS_RECEIVED  
    82. android.provider.Telephony.WAP_PUSH_RECEIVED  
    83. com.google.gservices.intent.action.GSERVICES_CHANGED 
    android.bluetooth.a2dp.intent.action.SINK_STATE_CHANGED
    android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED
    android.bluetooth.intent.action.BOND_STATE_CHANGED_ACTION
    android.bluetooth.intent.action.DISCOVERY_COMPLETED
    android.bluetooth.intent.action.DISCOVERY_STARTED
    android.bluetooth.intent.action.HEADSET_ADUIO_STATE_CHANGED
    android.bluetooth.intent.action.HEADSET_STATE_CHANGED
    android.bluetooth.intent.action.NAME_CHANGED
    android.bluetooth.intent.action.PAIRING_CANCEL
    android.bluetooth.intent.action.PAIRING_REQUEST
    android.bluetooth.intent.action.REMOTE_DEVICE_CONNECTED
    android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
    android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
    android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECTED
    android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECT_REQUESTED
    android.bluetooth.intent.action.REMOTE_DEVICE_FOUND
    android.bluetooth.intent.action.REMOTE_NAME_FAILED
    android.bluetooth.intent.action.REMOTE_NAME_UPDATED
    android.bluetooth.intent.action.SCAN_MODE_CHANGED
    android.intent.action.AIRPLANE_MODE
    android.intent.action.BATTERY_CHANGED
    android.intent.action.BATTERY_LOW
    android.intent.action.BOOT_COMPLETED
    android.intent.action.CAMERA_BUTTON
    android.intent.action.CONFIGURATION_CHANGED
    android.intent.action.DATA_SMS_RECEIVED
    android.intent.action.DATE_CHANGED
    android.intent.action.DEVICE_STORAGE_LOW
    android.intent.action.DEVICE_STORAGE_OK
    android.intent.action.GTALK_CONNECTED
    android.intent.action.GTALK_DISCONNECTED
    android.intent.action.HEADSET_PLUG
    android.intent.action.INPUT_METHOD_CHANGED
    android.intent.action.MANAGE_PACKAGE_STORAGE
    android.intent.action.MEDIA_BAD_REMOVAL
    android.intent.action.MEDIA_BUTTON
    android.intent.action.MEDIA_CHECKING
    android.intent.action.MEDIA_EJECT
    android.intent.action.MEDIA_MOUNTED
    android.intent.action.MEDIA_NOFS
    android.intent.action.MEDIA_REMOVED
    android.intent.action.MEDIA_SCANNER_FINISHED
    android.intent.action.MEDIA_SCANNER_SCAN_FILE
    android.intent.action.MEDIA_SCANNER_STARTED
    android.intent.action.MEDIA_SHARED
    android.intent.action.MEDIA_UNMOUNTABLE
    android.intent.action.MEDIA_UNMOUNTED
    android.intent.action.NEW_OUTGOING_CALL
    android.intent.action.PACKAGE_ADDED
    android.intent.action.PACKAGE_CHANGED
    android.intent.action.PACKAGE_DATA_CLEARED
    android.intent.action.PACKAGE_INSTALL
    android.intent.action.PACKAGE_REMOVED
    android.intent.action.PACKAGE_REPLACED
    android.intent.action.PACKAGE_RESTARTED
    android.intent.action.PHONE_STATE
    android.intent.action.PROVIDER_CHANGED
    android.intent.action.REBOOT
    android.intent.action.SCREEN_OFF
    android.intent.action.SCREEN_ON
    android.intent.action.TIMEZONE_CHANGED
    android.intent.action.TIME_SET
    android.intent.action.TIME_TICK
    android.intent.action.UID_REMOVED
    android.intent.action.UMS_CONNECTED
    android.intent.action.UMS_DISCONNECTED
    android.intent.action.USER_PRESENT
    android.intent.action.WALLPAPER_CHANGED
    android.media.AUDIO_BECOMING_NOISY
    android.media.RINGER_MODE_CHANGED
    android.media.VIBRATE_SETTING_CHANGED
    android.net.conn.BACKGROUND_DATA_SETTING_CHANGED
    android.net.wifi.NETWORK_IDS_CHANGED
    android.net.wifi.RSSI_CHANGED
    android.net.wifi.SCAN_RESULTS
    android.net.wifi.STATE_CHANGE
    android.net.wifi.WIFI_STATE_CHANGED
    android.net.wifi.supplicant.CONNECTION_CHANGE
    android.net.wifi.supplicant.STATE_CHANGE
    android.provider.Telephony.SIM_FULL
    android.provider.Telephony.SMS_RECEIVED
    android.provider.Telephony.WAP_PUSH_RECEIVED
    com.google.gservices.intent.action.GSERVICES_CHANGED

    Api Level 4增加的:(SDK 1.6)

    Xml代码 复制代码

    1. android.intent.action.ACTION_POWER_CONNECTED  
    2. android.intent.action.ACTION_POWER_DISCONNECTED  
    3. android.intent.action.ACTION_SHUTDOWN  
    4. android.intent.action.BATTERY_OKAY  
    5. android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED  
    6. android.speech.tts.engine.TTS_DATA_INSTALLED  
    7. com.google.gservices.intent.action.GSERVICES_OVERRIDE 
    android.intent.action.ACTION_POWER_CONNECTED
    android.intent.action.ACTION_POWER_DISCONNECTED
    android.intent.action.ACTION_SHUTDOWN
    android.intent.action.BATTERY_OKAY
    android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED
    android.speech.tts.engine.TTS_DATA_INSTALLED
    com.google.gservices.intent.action.GSERVICES_OVERRIDE

    Api Level 5(SDK 2.0)蓝牙部分全部修改,修改后的

    Xml代码 复制代码

    1. android.bluetooth.a2dp.action.SINK_STATE_CHANGED  
    2. android.bluetooth.adapter.action.DISCOVERY_FINISHED  
    3. android.bluetooth.adapter.action.DISCOVERY_STARTED  
    4. android.bluetooth.adapter.action.LOCAL_NAME_CHANGED  
    5. android.bluetooth.adapter.action.SCAN_MODE_CHANGED  
    6. android.bluetooth.adapter.action.STATE_CHANGED  
    7. android.bluetooth.device.action.ACL_CONNECTED  
    8. android.bluetooth.device.action.ACL_DISCONNECTED  
    9. android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED  
    10. android.bluetooth.device.action.BOND_STATE_CHANGED  
    11. android.bluetooth.device.action.CLASS_CHANGED  
    12. android.bluetooth.device.action.FOUND  
    13. android.bluetooth.device.action.NAME_CHANGED  
    14. android.bluetooth.devicepicker.action.DEVICE_SELECTED  
    15. android.bluetooth.devicepicker.action.LAUNCH  
    16. android.bluetooth.headset.action.AUDIO_STATE_CHANGED  
    17. android.bluetooth.headset.action.STATE_CHANGED 
    android.bluetooth.a2dp.action.SINK_STATE_CHANGED
    android.bluetooth.adapter.action.DISCOVERY_FINISHED
    android.bluetooth.adapter.action.DISCOVERY_STARTED
    android.bluetooth.adapter.action.LOCAL_NAME_CHANGED
    android.bluetooth.adapter.action.SCAN_MODE_CHANGED
    android.bluetooth.adapter.action.STATE_CHANGED
    android.bluetooth.device.action.ACL_CONNECTED
    android.bluetooth.device.action.ACL_DISCONNECTED
    android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED
    android.bluetooth.device.action.BOND_STATE_CHANGED
    android.bluetooth.device.action.CLASS_CHANGED
    android.bluetooth.device.action.FOUND
    android.bluetooth.device.action.NAME_CHANGED
    android.bluetooth.devicepicker.action.DEVICE_SELECTED
    android.bluetooth.devicepicker.action.LAUNCH
    android.bluetooth.headset.action.AUDIO_STATE_CHANGED
    android.bluetooth.headset.action.STATE_CHANGED

    然后增加的有

    Xml代码 复制代码

    1. android.intent.action.DOCK_EVENT  
    2. android.provider.Telephony.SMS_REJECTED 
    android.intent.action.DOCK_EVENT
    android.provider.Telephony.SMS_REJECTED

    Api Level 6(SDK 2.0.1)没有改变。

    Api Level 7(SDK 2.1)增加的:

    Xml代码 复制代码

    1. android.intent.action.LOCALE_CHANGED 
    android.intent.action.LOCALE_CHANGED

    Api Level 8(SDK 2.2)增加的:

    Xml代码 复制代码

    1. android.app.action.ACTION_PASSWORD_CHANGED  
    2. android.app.action.ACTION_PASSWORD_FAILED  
    3. android.app.action.ACTION_PASSWORD_SUCCEEDED  
    4. android.app.action.DEVICE_ADMIN_DISABLED  
    5. android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED  
    6. android.app.action.DEVICE_ADMIN_ENABLED  
    7. android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE  
    8. android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE  
    9. android.media.SCO_AUDIO_STATE_CHANGED 
    android.app.action.ACTION_PASSWORD_CHANGED
    android.app.action.ACTION_PASSWORD_FAILED
    android.app.action.ACTION_PASSWORD_SUCCEEDED
    android.app.action.DEVICE_ADMIN_DISABLED
    android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED
    android.app.action.DEVICE_ADMIN_ENABLED
    android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE
    android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE
    android.media.SCO_AUDIO_STATE_CHANGED
    展开全文
  • android Broadcast广播

    2015-06-24 15:20:38
    android Broadcast广播的简单总结

    一子类创建

    使用BroadcastReceiver的子类,重写onReceive(Context context, Intent intent)方法

     

    二注册广播

    代码方式:

    IntentFilterfilter = new IntentFilter((Stringaction);

    Context的 registerReceiver(BroadcastReceiver子类对象,filter);

    解除注册Context  unregisterReceiver(BroadcastReceiver子类对象);

    配置文件方式

    优先级范围-1000~~1000 越大权限越高

    <receiver android:name=".className">

                <intent-filter android:priority="优先级">

                    <action android:name="user-defined"/>

                </intent-filter>

    </receiver>

     

    三触发广播

    Intent对象设置setAction(Stringaction);

    sendBroadcast(intent)触发action对应的BroadcastReceiver,执行里面的onReceive方法

    sendOrderedBroadcast(intent,receiverPermission);发送有序广播,第二个参数可为空

     

     

    BroadcastReceiver的onReceive方法需在10s内完成,所以不能有耗时操作,建议Service,开线程不太好

    onReceive(Context context, Intent intent)的intent可以存取数据

    监听系统Broadcast Action道理相似

     

     

     

     

     

     

    展开全文
  • android Broadcast学习

    万次阅读 2011-04-19 10:31:00
    android Broadcast学习 其实所谓的静态注册,动态注册,是指接收广播的时候,是静态注册接收还是动态注册接收,发送的时候不分静态,动态 以发送intent为例, 一共分4种情况,以每次注册两个...
    
    

    其实所谓的静态注册,动态注册,是指接收广播的时候,是静态注册接收还是动态注册接收,发送的时候不分静态,动态

    以发送intent为例,

    一共分4种情况,以每次注册两个Broadcast为例:

    情况一,注册2个静态Broadcast

    如果是静态注册的,接收的一定是某一个类继承BroadcastReceiver

    2个java文件如下:

    BroadcastActivity.java
    代码
    package com.broad.test1;

    import
    android.app.Activity;
    import
    android.content.Intent;
    import
    android.os.Bundle;
    import
    android.widget.Button;
    import
    android.view.View;
    import
    android.view.View.OnClickListener;
    import
    android.util.Log;

    //2个静态注册

    public class BroadcastActivity extends Activity {
    /** Called when the activity is first created. */

    private Button button01;
    private
    Button button02;
    private static final String TAG = "BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1 = "su1.bluetooth.true"
    ;
    private static final String INTENAL_ACTION_2 = "su1.bluetooth.false"
    ;

    @Override
    public void
    onCreate(Bundle savedInstanceState) {
    super
    .onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button01
    =
    (Button) findViewById(R.id.button01);
    button02
    =
    (Button) findViewById(R.id.button02);
    button01.setOnClickListener(mybtnListener);
    button02.setOnClickListener(mybtnListener);
    }

    private OnClickListener mybtnListener = new
    OnClickListener() {

    @Override
    public void
    onClick(View v) {
    // TODO Auto-generated method stub

    if (v.getId() == (R.id.button01)) {
    Intent intent
    = new
    Intent(INTENAL_ACTION_1);
    //
    也可以用mContext.sendBroadcast(intent),mContext.sendBroadcast可以在构造函数里面初始化,
    // 有内部类的时候必须用???

    sendBroadcast(intent);
    }
    else if (v.getId() ==
    (R.id.button02)) {
    Intent intent
    = new
    Intent(INTENAL_ACTION_2);
    sendBroadcast(intent);
    }
    else
    {
    Log.v(TAG,
    "not true not false"
    );
    }
    }

    };

    }

    BroadcastRecTest.java
    代码
    package com.broad.test1;

    import
    android.content.BroadcastReceiver;

    import
    android.content.Context;
    import
    android.content.Intent;
    import
    android.util.Log;
    public class BroadcastRecTest extends
    BroadcastReceiver {
    private static final String TAG="BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1="su1.bluetooth.true"
    ;
    private static final String INTENAL_ACTION_2="su1.bluetooth.false"
    ;
    @Override
    public void
    onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    String action = intent.getAction();
    if
    (action.equals(INTENAL_ACTION_1)){
    Log.d(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>>>su1.bluetooth.true"
    );
    }
    else if
    (action.equals(INTENAL_ACTION_2)){
    Log.d(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>su1.bluetooth.false"
    );
    }
    else
    {
    Log.d(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>other type 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"

    />
    <Button
    android:id="@+id/button01"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="静态接收广播true"/>
    <Button
    android:id="@+id/button02"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="静态接收广播false"/>
    </LinearLayout>

    AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.broad.test1"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".BroadcastActivity"
    android:label
    ="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <receiver android:name="BroadcastRecTest">
    <intent-filter>
    <action android:name="su1.bluetooth.true" />
    <action android:name="su1.bluetooth.false" />
    </intent-filter>
    </receiver>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    </manifest>

    运行结果:

    注意,静态注册的broadcast运行有点慢:

    情况2,一个静态,一个动态注册Broadcast

    BroadcastActivity.java
    代码
    package com.broad.test1;

    import
    android.app.Activity;
    import
    android.content.BroadcastReceiver;
    import
    android.content.Context;
    import
    android.content.Intent;
    import
    android.content.IntentFilter;
    import
    android.os.Bundle;
    import
    android.widget.Button;
    import
    android.view.View;
    import
    android.view.View.OnClickListener;
    import
    android.util.Log;
    //1个动态,1个静态注册

    public class BroadcastActivity extends Activity {
    /** Called when the activity is first created. */

    private Button button01;
    private
    Button button02;
    private static final String TAG="BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1="su2.bluetooth.true"
    ;
    private static final String INTENAL_ACTION_2="su2.bluetooth.false"
    ;
    @Override
    public void
    onCreate(Bundle savedInstanceState) {
    super
    .onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button01
    =
    (Button)findViewById(R.id.button01);
    button02
    =
    (Button)findViewById(R.id.button02);
    button01.setOnClickListener(mybtnListener);
    button02.setOnClickListener(mybtnListener);
    }
    private OnClickListener mybtnListener=new
    OnClickListener(){

    @Override
    public void
    onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId()==(R.id.button01)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_1);
    sendBroadcast(intent);
    }
    else if(v.getId()==
    (R.id.button02)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_2);
    sendBroadcast(intent);
    }
    else
    {
    Log.v(TAG,
    "not true not false"
    );
    }
    }

    };


    // 接收动态注册广播的BroadcastReceiver

    private BroadcastReceiver bcrIntenal2 = new BroadcastReceiver() {
    @Override
    public void
    onReceive(Context context, Intent intent) {
    //
    TODO Auto-generated method stub
    //
    如果有多个动态注册的广播,要用if(action.equals()){}else if(action.equals()){}来判断是哪个
    //
    String action = intent.getAction();
    //这里只有一个,不用判断了。

    Log.v(TAG,"surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su2.bluetooth.false");
    }
    };
    //在onStart中动态注册广播,当然也可以在onCreate里面注册

    @Override
    protected void
    onStart() {
    // TODO Auto-generated method stub

    super.onStart();
    //动态注册

    IntentFilter intentFilter= new IntentFilter(INTENAL_ACTION_2);
    //也可以用这种方法动态注册多个
    ,说明我可以”接收“这么多的动态广播。
    /*
    IntentFilter intentFilter= new IntentFilter();
    intentFilter.addAction("action1");
    intentFilter.addAction("action2");
    */

    registerReceiver(bcrIntenal2,intentFilter);
    }

    //在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。
    @Override
    protected void
    onStop() {
    // TODO Auto-generated method stub

    super.onStop();
    unregisterReceiver(bcrIntenal2);
    }

    }
    BroadcastRecTest.java
    代码
    package com.broad.test1;

    import
    android.content.BroadcastReceiver;

    import
    android.content.Context;
    import
    android.content.Intent;
    import
    android.util.Log;
    public class BroadcastRecTest extends
    BroadcastReceiver {
    private static final String TAG="BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1="su2.bluetooth.true"
    ;
    @Override
    public void
    onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    String action = intent.getAction();
    if
    (action.equals(INTENAL_ACTION_1)){
    Log.d(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>>>su.bluetooth.true"
    );
    }
    else
    {
    Log.d(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>other type intent--jingtai"
    );
    }

    }
    }

    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"

    />
    <Button
    android:id="@+id/button01"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="静态接收广播true"/>
    <Button
    android:id="@+id/button02"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="动态接收广播false"/>
    </LinearLayout>

    AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.broad.test1"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".BroadcastActivity"
    android:label
    ="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <receiver android:name="BroadcastRecTest">
    <intent-filter>
    <action android:name="su2.bluetooth.true" />
    </intent-filter>
    </receiver>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    </manifest>

    true是静态注册的,在Android.xml中注册的,false是动态注册的,在java代码中注册的

    运行结果:

    情况三、两个都是动态注册的,在同一个Activity里面既发送,又接收,(当然也可以在不同的java中,一个发送,另一个java接收,哪怕是不同的包)

    动态注册不需要一个类继承BroadcastReceiver这个类了,直接new一个对象即可

    代码
    package com.broad.test1;

    import
    android.app.Activity;
    import
    android.content.BroadcastReceiver;
    import
    android.content.Context;
    import
    android.content.Intent;
    import
    android.content.IntentFilter;
    import
    android.os.Bundle;
    import
    android.widget.Button;
    import
    android.view.View;
    import
    android.view.View.OnClickListener;
    import
    android.util.Log;

    //两个动态注册

    public class BroadcastActivity extends Activity {
    /** Called when the activity is first created. */

    private Button button01;
    private
    Button button02;
    private static final String TAG="BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1="su3.bluetooth.true"
    ;
    private static final String INTENAL_ACTION_2="su3.bluetooth.false"
    ;
    // private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;

    @Override
    public void
    onCreate(Bundle savedInstanceState) {
    super
    .onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button01
    =
    (Button)findViewById(R.id.button01);
    button02
    =
    (Button)findViewById(R.id.button02);
    button01.setOnClickListener(mybtnListener);
    button02.setOnClickListener(mybtnListener);
    }
    private OnClickListener mybtnListener=new
    OnClickListener(){

    @Override
    public void
    onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId()==(R.id.button01)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_1);
    sendBroadcast(intent);
    }
    else if(v.getId()==
    (R.id.button02)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_2);
    sendBroadcast(intent);
    }
    else
    {
    Log.v(TAG,
    "not true not false"
    );
    }
    }

    };

    // 接收动态注册广播的BroadcastReceiver

    private BroadcastReceiver bcrIntenal2 = new BroadcastReceiver() {
    @Override
    public void
    onReceive(Context context, Intent intent) {
    //
    TODO Auto-generated method stub
    //如果有多个动态注册的广播,要用if(action.equals()){}else if(action.equals()){}来判断是哪个

    String action = intent.getAction();
    if
    (action.equals(INTENAL_ACTION_1)){
    Log.v(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.true"
    );
    }
    else if
    (action.equals(INTENAL_ACTION_2)){
    Log.v(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su3.bluetooth.false"
    );
    }

    }
    };
    //在onStart中动态注册广播,当然也可以在onCreate里面注册

    @Override
    protected void
    onStart() {
    // TODO Auto-generated method stub

    super.onStart();
    //动态注册两个

    IntentFilter intentFilter= new
    IntentFilter();
    intentFilter.addAction(INTENAL_ACTION_1);
    intentFilter.addAction(INTENAL_ACTION_2);
    registerReceiver(bcrIntenal2,intentFilter);
    }

    //在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。

    @Override
    protected void
    onStop() {
    // TODO Auto-generated method stub

    super.onStop();
    unregisterReceiver(bcrIntenal2);
    }

    }

    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"

    />
    <Button
    android:id="@+id/button01"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="动态接收广播true"/>
    <Button
    android:id="@+id/button02"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="动态接收广播false"/>
    </LinearLayout>

    AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.broad.test1"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".BroadcastActivity"
    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" />
    </manifest>

    运行结果:

    情况4,sendBroadcast的时候加权限,以2个都动态注册为例

    代码
    package com.broad.test1;

    import
    android.app.Activity;
    import
    android.content.BroadcastReceiver;
    import
    android.content.Context;
    import
    android.content.Intent;
    import
    android.content.IntentFilter;
    import
    android.os.Bundle;
    import
    android.widget.Button;
    import
    android.view.View;
    import
    android.view.View.OnClickListener;
    import
    android.util.Log;

    //两个动态注册,sendBroadcast的时候加权限

    public class BroadcastActivity extends Activity {
    /** Called when the activity is first created. */

    private Button button01;
    private
    Button button02;
    private static final String TAG="BroadcastActivity"
    ;
    private static final String INTENAL_ACTION_1="su4.bluetooth.true"
    ;
    private static final String INTENAL_ACTION_2="su4.bluetooth.false"
    ;
    private static final String BLUETOOTH_PERM =
    android.Manifest.permission.BLUETOOTH;
    @Override
    public void
    onCreate(Bundle savedInstanceState) {
    super
    .onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button01
    =
    (Button)findViewById(R.id.button01);
    button02
    =
    (Button)findViewById(R.id.button02);
    button01.setOnClickListener(mybtnListener);
    button02.setOnClickListener(mybtnListener);
    }
    private OnClickListener mybtnListener=new
    OnClickListener(){

    @Override
    public void
    onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId()==(R.id.button01)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_1);
    sendBroadcast(intent,BLUETOOTH_PERM);
    }
    else if(v.getId()==
    (R.id.button02)){
    Intent intent
    = new
    Intent(INTENAL_ACTION_2);
    sendBroadcast(intent,BLUETOOTH_PERM);
    }
    else
    {
    Log.v(TAG,
    "not true not false"
    );
    }
    }

    };

    // 接收动态注册广播的BroadcastReceiver

    private BroadcastReceiver bcrIntenal2 = new BroadcastReceiver() {
    @Override
    public void
    onReceive(Context context, Intent intent) {
    //
    TODO Auto-generated method stub
    //如果有多个动态注册的广播,要用if(action.equals()){}else if(action.equals()){}来判断是哪个

    String action = intent.getAction();
    if
    (action.equals(INTENAL_ACTION_1)){
    Log.v(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.true"
    );
    }
    else if
    (action.equals(INTENAL_ACTION_2)){
    Log.v(TAG,
    "surx>>>>>>>>>>>>>>>>>>>>>>>>>>>dong--su4.bluetooth.false"
    );
    }

    }
    };
    //在onStart中动态注册广播,当然也可以在onCreate里面注册

    @Override
    protected void
    onStart() {
    // TODO Auto-generated method stub

    super.onStart();
    //动态注册两个
    ,说明我可以”接收“2个这样Action的动态广播
    IntentFilter intentFilter= new
    IntentFilter();
    intentFilter.addAction(INTENAL_ACTION_1);
    intentFilter.addAction(INTENAL_ACTION_2);
    registerReceiver(bcrIntenal2,intentFilter);
    }

    //在onStop中取消注册广播,如果在onCreate里面注册,那么在onDestroy里面取消注册。

    @Override
    protected void
    onStop() {
    // TODO Auto-generated method stub

    super.onStop();
    unregisterReceiver(bcrIntenal2);
    }

    }

    发送的时候加了蓝牙权限,

    若在使用sendBroadcast()的方法是指定了接收权限,则只有接收方的包内--在AndroidManifest.xml中用<uses- permission>标签声明了拥有此权限的BroascastReceiver才会有可能接收到发送来的Broadcast。

    同样,若在注册BroadcastReceiver时指定了可接收的Broadcast的权限,则只有在接收方的包内的AndroidManifest.xml中 用<uses-permission>标签声明了,拥有此权限的Context对象所发送的Broadcast才能被这个 BroadcastReceiver所接收。

    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"

    />
    <Button
    android:id="@+id/button01"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="动态接收广播true"/>
    <Button
    android:id="@+id/button02"

    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="动态接收广播false"/>
    </LinearLayout>

    AndroidManifest.xml

    代码
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="com.broad.test1"
    android:versionCode
    ="1"
    android:versionName
    ="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".BroadcastActivity"
    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.BLUETOOTH"/>
    </manifest>

    注意上面加的蓝牙permission权限

    运行结果:

    总结:

    以发送intent为例,发送广播都用sendBroadcast(intent);

    静态注册接收的时候,只需要

    (1)得到action,如:

    String action = intent.getAction();

    (2)判断action类型,做出相应的动作。

    动态注册接收的时候,需要:

    (1) IntentFilter intentFilter= new IntentFilter();

    (2)intentFilter.addAction(。。);可以添加多个

    (3)注册接收,registerReceiver(BroadcastReceiver对象,intentFilter);别忘了在其他需要的地方取消注册接收!!!

    (4)得到action,如:

     
    String action = intent.getAction();
    (5)
    判断action类型,做出相应的动作。
    展开全文
  • Android Broadcast用法

    千次阅读 2018-03-31 17:50:29
    Android 系统中,广播(Broadcast)是在组件之间传播数据的一种机制,这些组件可以位于不同的进程中,起到进程间通信的作用 BroadcastReceiver 是对发送出来的 Broadcast 进行过滤、接受和响应的组件。首先将要...
  • Android Broadcast 笔记

    千次阅读 2015-04-16 19:20:14
    Broadcast Receiver  Broadcast receiver和activity、service组件一样都是接收intent的组件。一收到匹配的broadcast intent,broadcast receiver的onReceive(Context,Intent)方法立即运行,然后broadcast receiver...
  • Android Broadcast机制

    千次阅读 2011-12-02 14:51:17
     在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。我们拿广播电台来做个比方。我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内容,而我们用户只需要将...
  • Android BroadCast Types

    2014-03-07 15:22:00
    Types :- Local,Normal,Ordered and Sticky ...Normal Broadcast :- use sendBroadcast() :- asynchronous broadcast :- any receiver receives broadcast not any particular order Ordered Broadc
  • 测试主程序BaseReceiver.javapackage com.chy.broadcastdemo;import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;//广播接收器的生命周期只有十秒左右,...
  • In addition to providing a mechanism for launching application activities, intents are also used as a way to broadcast system wide messages to other components on the system. This involves the impleme
  • android broadcast用法

    千次阅读 2013-03-12 20:08:58
    转自:... 在 Android 中使用 Activity, Service, Broadcast, BroadcastReceiver 活动(Activity) - 用于表现功能 服务(Service) - 相当于后台运行的 Activity 广播(Broadca
  • Android Broadcast timeout

    千次阅读 2017-04-20 15:30:19
    Broadcast timeout Brocadcast timeout 是指串行有序广播发送给receiver的时候,app没有来得及处理这个广播,或者app的receiver处理这个广播的时候超时了(前台广播10s,后台广播60s),没有及时回调finishReceiver...
  • Android Broadcast Receiver 基础详解

    千次阅读 2016-08-29 10:15:21
    Android中的广播机制十分灵活,因为Android中的每个应用程序都可以对自己感兴趣的广播进行注册。它提供了一套完整的API允许应用程序自由的发送和接受广播。 广播分为两类:1.标准广播,是一种完全异步执行的广播,在...
  • android broadcast机制

    千次阅读 2013-05-18 10:17:51
    图解 Android 广播机制 从现实生活中理解广播机制 一听到广播我们第一感觉就会联想到小时候村里面的广播,每逢村里有什么活动都是通过广播发送的。收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播...
  • broadcast 广播当系统变化(来电,网路连接),android系统会利用intent进行广播。如果 注册了这个事件的监听(broadcast receiver)就能处理这些事情 1.程序自己广播  Intent intent = new Intent("name");
  • Android Broadcast 小例子

    千次阅读 2012-07-26 22:41:40
    懒得写了直接粘上代码吧做个备份 manifest.xml   <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dada.test" ... android:versionCode="1" ... android:versionName="1.0
  • 80 Android BroadcastReciver

    2014-01-19 10:53:10
    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" andr
  • android broadcast intent flags解析

    千次阅读 2018-12-07 18:59:04
    发送广播设置flags值, 指定特定接收者 Intent intent = new Intent(BluetoothHeadsetClient.ACTION_CALL_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); sendBroadcast(intent, ProfileService....
  • Android Broadcast发送和接受消息

    千次阅读 2014-09-23 16:13:35
    Broadcast.java package com.example.broadcast; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickLi

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 56,172
精华内容 22,468
关键字:

androidbroadcast