dynamic注册vs BroadcastReceiver的静态注册

我们所有人都知道我们注册了两种types的BroadcastReceiver

1)Static Registration

2)Dynamic Registration

但我的疑问是,当我们需要使用Static ,当我们需要使用Dynamic

Solutions Collecting From Web of "dynamic注册vs BroadcastReceiver的静态注册"

我们知道有两种方法可以注册BroadcastReceiver ; 一个是静态的 ,另一个是dynamic的

静态的:

  1. 在你的Manifest文件中使用标签。 (AndroidManifest.xml中)
  2. 并非所有事件都可以静态注册。
  3. 有些事件需要权限。

dynamic:

  1. 使用Context.registerReceiver()来dynamic注册一个实例。
  2. 注意:暂停时取消注册。

当我们做dynamic注册(即在运行时),它将与应用程序的生命周期相关联。 如果我们这样做静态注册(即在编译时),我们的应用程序没有运行,将创build一个新的进程来处理广播。

1)静态注册

实现是明显的,android系统可以启动进程并运行你的boardcast接收器。 一个例子就像你想要更新你的数据,当一个新的意图来自系统等等。你也需要小心安全问题。

2)dynamic注册

在java代码中实现,boardcast接收器只在你的应用程序运行到注册行时运行。 因此,如果您只想在特定条件下调出板载接收器,则主要想使用此function。

我将通过编码向您展示不同的静态和dynamic广播接收器:

a)为这两种收件人定义用户界面:

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.broadcastreceiverdemo.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="registerBroadcastReceiverDynamically" android:text="Register Broadcast Receiver Dynamically" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendUsingDynamicallyRegisteredBroadcastReceiver" android:text="Send Broadcast Msg Dynamically" /> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:onClick="sendUsingStaticallyRegisteredBroadcastReceiver" android:text="Send Broadcast Statically" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </RelativeLayout> 

b)DynamicBroadcastReceiver.java

  public class DynamicBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Dynamic Broadcast", Toast.LENGTH_SHORT).show(); } 

}

c)StaticBroadcastReceiver.java

 public class StaticBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Static Broadcast", Toast.LENGTH_SHORT).show(); } 

}

d)MainActivity.java

  public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //////////////////=================Starts Dynamic Broadcast Receiver DynamicBroadcastReceiver dynamicBroadcastReceiver = new DynamicBroadcastReceiver(); public void registerBroadcastReceiverDynamically(View view) { IntentFilter filter = new IntentFilter(); filter.addAction("MY_BROADCAST"); registerReceiver(dynamicBroadcastReceiver, filter); } public void sendUsingDynamicallyRegisteredBroadcastReceiver(View view) { Intent i = new Intent(); i.setAction("MY_BROADCAST"); sendBroadcast(i); } @Override protected void onDestroy() { if (dynamicBroadcastReceiver != null) { unregisterReceiver(dynamicBroadcastReceiver); } super.onDestroy(); } //////////////////=================Ends Dynamic Broadcast Receiver //////////////////=================Starts Static Broadcast Receiver public void sendUsingStaticallyRegisteredBroadcastReceiver(View view) { Intent i = new Intent(); i.setAction("MY_BROADCAST_STATIC"); sendBroadcast(i); } 

}

e)清单文件:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.broadcastreceiverdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".DynamicBroadcastReceiver"> </receiver> <receiver android:name=".StaticBroadcastReceiver"> <intent-filter> <action android:name="MY_BROADCAST_STATIC" /> </intent-filter> </receiver> </application> </manifest>