静态注册和动态注册的区别 1)静态注册:在 AndroidManifest.xml注册,android不能自动销毁广播接收器,也就是说当应用程序关闭后,还是会接收广播。 2)动态注册:在代码中通过registerReceiver()手工注册.当程序关闭时,该接收器也会随之销毁。当然,也可手工调用unregisterReceiver()进行销毁。
package com.example.broadcast_2;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button btn1,btn2,btn3;Intent intent = new Intent();    BroadCastTest   castTest = new BroadCastTest();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//btn1,btn2,btn3;btn1=(Button)findViewById(R.id.button1);btn2=(Button)findViewById(R.id.button2);btn3=(Button)findViewById(R.id.button3);    btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根intent.setAction("static_broadcast");intent.putExtra("name", "静态的");sendBroadcast(intent);}});    btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("dynamic_broadcast");MainActivity.this.registerReceiver(castTest, intentFilter);intent.setAction("dynamic_broadcast");intent.putExtra("name", "动态的");sendBroadcast(intent);}});    btn3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根unregisterReceiver(castTest);finish();}});}///onCreate}
package com.example.broadcast_2;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class BroadCastTest  extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO 自动生成的方法存根String name = intent.getStringExtra("name");Toast.makeText(context, name+"广播", Toast.LENGTH_SHORT).show();}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.broadcast_2"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="15"        android:targetSdkVersion="16" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <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>        <receiver android:name=".BroadCastTest" >            <intent-filter android:priority="3" >                <action android:name="static_broadcast" />            </intent-filter>        </receiver>    </application></manifest>
<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"       android:background="#fff"      android:padding="10dp">        <LinearLayout          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:orientation="vertical"          android:gravity="center_horizontal" >            <Button              android:id="@+id/button1"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:text="静态注册广播" />            <Button              android:id="@+id/button2"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:text="动态注册广播" />            <Button              android:id="@+id/button3"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:text="退出" />        </LinearLayout>    </LinearLayout>  





更多相关文章

  1. Android(安卓)APP更新实践 - 完美解决9.0前的各种版本.md
  2. [置顶] android 耳机按钮深层理解
  3. 一个轻量级、可插拔的Android消息推送框架。一键集成推送(极光推
  4. 【android】使用Event Bus模式解耦Android(安卓)App组件间通信
  5. android2.2应用开发之IccCard(sim卡或USIM卡)第一篇
  6. android注册 登录+修改帐号密码+添加资料+给指定帐号充值
  7. 分享:Android中利用机器码注册机制防止破解
  8. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实
  9. Android(安卓)桌面组件【app widget】 进阶项目②--心情记录器

随机推荐

  1. Android(安卓)技巧:命令行运行 sqlite3
  2. Android-常用UI控件(Spinner/AutoComplet
  3. 【MarsChen】D03_Say_Hello_to_Android
  4. Android待机流程分析
  5. android webket的应用,webview,javascrip
  6. Android(安卓)获取地理位置的经度和纬度
  7. 申请 Android(安卓)MapView 的apiKey流程
  8. JDK ANT ANDROID Configure
  9. Android+JNI调用–文件操作
  10. GestureDetector.OnGestureListener 触摸