欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

BroadcastReceiver靜態(tài)注冊(cè)案例詳解

 更新時(shí)間:2022年08月26日 10:18:46   作者:磚廠打工仔  
這篇文章主要為大家詳細(xì)介紹了BroadcastReceiver靜態(tài)注冊(cè)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

BroadcastReceiver靜態(tài)注冊(cè)案例演示,供大家參考,具體內(nèi)容如下

靜態(tài)注冊(cè)與動(dòng)態(tài)注冊(cè)的區(qū)別:

動(dòng)態(tài)注冊(cè):廣播接收器可以自由的控制注冊(cè)與取消,具有很大的靈活性。但只有在應(yīng)用程序啟動(dòng)后才能收到廣播。并且動(dòng)態(tài)注冊(cè)的廣播接收器的生命周期與其對(duì)應(yīng)的Acitivity的生命周期是一致的。
靜態(tài)注冊(cè):又叫做清單注冊(cè),即在AndroidManifest.xml中進(jìn)行注冊(cè)。靜態(tài)注冊(cè)的廣播不受程序是否啟動(dòng)的約束,當(dāng)應(yīng)用程序關(guān)閉后,還可以接收到廣播。

效果圖:

代碼:

MainActivity.java

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? //Intent intent = new Intent("com.example.action.MY_BROADCAST");
? ? ? ? ? ? ? ? //sendBroadcast(intent);
? ? ? ? ? ? ? ? //Android 8.0后不再支持以上這種方式
? ? ? ? ? ? ? ? //需采用指定包名或組件名的形式,即顯式Intent

? ? ? ? ? ? ? ? //1.指定包名
? ? ? ? ? ? ? ? //Intent intent = new Intent();
? ? ? ? ? ? ? ? //intent.setAction("com.example.action.MY_BROADCAST");
? ? ? ? ? ? ? ? //intent.setPackage("com.example.a02staticregister");
? ? ? ? ? ? ? ? //sendBroadcast(intent);

? ? ? ? ? ? ? ? //2.指定組件名,依據(jù)intent的Component屬性
? ? ? ? ? ? ? ? Intent intent = new Intent();
? ? ? ? ? ? ? ? intent.setComponent(new ComponentName(MainActivity.this,StaticReceiver.class));
? ? ? ? ? ? ? ? //也可簡(jiǎn)寫成以下形式,常用寫法
? ? ? ? ? ? ? ? //Intent intent = new Intent(MainActivity.this,StaticReceiver.class);
? ? ? ? ? ? ? ? sendBroadcast(intent);
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

StaticReceiver.java

public class StaticReceiver extends BroadcastReceiver {

? ? @Override
? ? public void onReceive(Context context, Intent intent) {
? ? ? ? Toast.makeText(context,"StaticReceiver收到廣播了~~",Toast.LENGTH_LONG).show();
? ? }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
? ? package="com.example.a02staticregister">

? ? <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/Theme.BroadcastRecetiverTest">
? ? ? ? <activity
? ? ? ? ? ? android:name=".MainActivity"
? ? ? ? ? ? android:exported="true">
? ? ? ? ? ? <intent-filter>
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />

? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />
? ? ? ? ? ? </intent-filter>
? ? ? ? </activity>
? ? ? ??
? ? ? ? <receiver android:name=".StaticReceiver"
? ? ? ? ? ? android:exported="true">
? ? ? ? ? ? <intent-filter>
? ? ? ? ? ? ? ? <action android:name="com.example.action.MY_BROADCAST"/>
? ? ? ? ? ? </intent-filter>
? ? ? ? </receiver>
? ? </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity">

? ? <Button
? ? ? ? android:id="@+id/btn"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="發(fā)送自定義廣播"/>

</LinearLayout>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論