最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android BroadcastReceiver廣播簡(jiǎn)單使用

 更新時(shí)間:2021年04月22日 16:44:58   作者:是心無(wú)吖  
這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver廣播簡(jiǎn)單的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android BroadcastReceiver廣播使用的具體代碼,供大家參考,具體內(nèi)容如下

靜態(tài)的BroadcastReceiver

主要代碼

public class MyReceiver extends BroadcastReceiver {
    @Override
    //接受廣播時(shí)回調(diào)
    public void onReceive(Context context, Intent intent) {
        //接收廣播
      if(intent != null){
          //接收到是什么廣播
          String action = intent.getAction();
          Log.e("測(cè)試",action);
      }
    }
}

在AndroidManifest.xml里設(shè)置權(quán)限

<receiver android:name=".MyReceiver">
            <!--接受廣播類型-->
            <intent-filter>
                <!--開(kāi)機(jī)廣播-->
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <!--電量低廣播-->
                <action android:name="android.intent.action.BATTERY_LOW"/>
                <!--應(yīng)用卸載-->
                <action android:name="android.intent.action.PACKAGE_REMOVED"/>
                <!--應(yīng)用安裝-->
                <action android:name="android.intent.action.PACKAGE_INSTALL"/>
                <!--數(shù)據(jù)類型-->
                <data android:scheme="package"/>
            </intent-filter>
</receiver>

動(dòng)態(tài)的BroadcastReceiver

主要代碼

1.設(shè)置一個(gè)Java類繼承BroadcastReceiver

public class MyReceiverD extends BroadcastReceiver {

    @Override
    //接受廣播時(shí)回調(diào)(不能做耗時(shí)操作,必須開(kāi)子線程)
    public void onReceive(Context context, Intent intent) {
            //接收廣播
            if(intent != null){
                //接收到是什么廣播
                String action = intent.getAction();
                Log.e("測(cè)試",action);
            }
        }
    }

在AndroidManifest.xml里設(shè)置權(quán)限

<!--動(dòng)態(tài)注冊(cè)-->
        <receiver android:name=".MyReceiverD">
        //因?yàn)槭莿?dòng)態(tài)設(shè)置就不需要在里面設(shè)置別的了
</receiver>

3.MainActivity

//新建一個(gè)廣播接收器 動(dòng)態(tài)廣播
        receiverD = new MyReceiverD();
        //接收那種廣播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        intentFilter.addDataScheme("package");
        intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
        //注冊(cè)廣播接收器
        registerReceiver(receiverD,intentFilter);

        protected void onDestroy() {
        super.onDestroy();
        //取消注冊(cè)關(guān)閉接收器
        if (receiverD != null){
            unregisterReceiver(receiverD);
        }
    }

隨便卸載一個(gè)應(yīng)用控制臺(tái)就會(huì)顯示

自定義的BroadcastReceiver

1.還是準(zhǔn)備一個(gè)Java繼承BroadcastReceiver

public class MyReceiverD_zdy extends BroadcastReceiver {
    private TextView txt;
    public MyReceiverD_zdy(TextView txt) {
        this.txt = txt;
    }
    public MyReceiverD_zdy() {

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //接收廣播
        if(intent != null){
            //接收到是什么廣播
            String action = intent.getAction();
            Log.e("測(cè)試",action);
            //判斷是什么廣播,是否是自己自定義的廣播
            if (TextUtils.equals(action,MainActivity.MY_ACTION)){
                //獲取廣播攜帶的數(shù)據(jù)
                String content = intent.getStringExtra(MainActivity.BROADCAST_CONTENT);
               if (txt != null){
               txt.setText("接收到的action是:"+action+"\n接收到的內(nèi)容是"+content);
               }
            }
        }
    }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="請(qǐng)輸入發(fā)送內(nèi)容:"/>

    <EditText
        android:id="@+id/etxt"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="16dp"
        />

    <Button
        android:id="@+id/bnt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_gravity="center_horizontal"
        android:text="發(fā)送廣播"/>

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="收到的內(nèi)容:"/>

</LinearLayout>

3.MainActivity

public class MainActivity extends AppCompatActivity {
    private MyReceiverD receiverD;
    private MyReceiverD_zdy receiverDZdy;
    private Button bnt;
    private EditText etxt;
    private TextView txt;
    public static final String MY_ACTION = "com.example.my";
    public static final String BROADCAST_CONTENT = "cs";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        
        //設(shè)置應(yīng)用主頁(yè)面的標(biāo)題
        setTitle(getPackageName());
       //新建廣播接收器
        receiverDZdy = new MyReceiverD_zdy(txt);
        //注冊(cè)廣播接收器

        //為廣播添加Action
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action,PACKAGE_REMOVED");
        //自定義
        intentFilter.addAction(MY_ACTION);
        //注冊(cè)廣播接收器
        registerReceiver(receiverDZdy,intentFilter);
        bnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //新建廣播 自定義
                Intent intent = new Intent(MY_ACTION);
                //攜帶數(shù)據(jù)
                intent.putExtra(BROADCAST_CONTENT,etxt.getText().toString());

                //發(fā)送廣播
                sendBroadcast(intent);
            }
        });
    }
    protected void onDestroy() {
        super.onDestroy();
        //取消注冊(cè)關(guān)閉接收器
        if (receiverDZdy != null){
            unregisterReceiver(receiverDZdy);
        }
    }

    private void initView() {
        //初始化
        etxt = (EditText) findViewById(R.id.etxt);
        txt =(TextView) findViewById(R.id.txt);
        bnt =(Button) findViewById(R.id.bnt);
    }
}

樣式

當(dāng)然也可以實(shí)現(xiàn)不同app接受發(fā)送的廣播內(nèi)容
復(fù)制代碼換app名字,當(dāng)前app發(fā)送的廣播新的app也可以接收到

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

相關(guān)文章

最新評(píng)論

聂拉木县| 衡阳市| 密山市| 茶陵县| 华阴市| 陕西省| 邯郸市| 伊宁县| 大冶市| 灵丘县| 微博| 旬邑县| 策勒县| 贵州省| 萝北县| 疏勒县| 通化市| 宁陕县| 葵青区| 永靖县| 枣庄市| 当涂县| 温泉县| 亳州市| 天等县| 岑溪市| 曲沃县| 西昌市| 兴山县| 朝阳市| 乐陵市| 安丘市| 昌江| 岱山县| 长沙市| 潢川县| 美姑县| 绥滨县| 长顺县| 广州市| 清镇市|