zoukankan      html  css  js  c++  java
  • 10-Android 广播接收器 BroadcastReceiver

    01-使用 BroadcastReceiver:

    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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnSendMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="发送消息" />
    
    </LinearLayout>

    MainActivity.java:

    package com.example.learnbroadcastreceiver;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //1.2=================================================
            findViewById(R.id.btnSendMsg).setOnClickListener(this);
            //====================================================
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btnSendMsg:
    
                    //1.4=================================================
    
                    Intent i = new Intent(this,MyReceiver.class);
                    i.putExtra("data","jiekexueyuan");
                    sendBroadcast(i);
                    //====================================================
    
    
    
    
                    //1.3===================================================
    //                sendBroadcast(new Intent(this,MyReceiver.class));
            }       //======================================================
        }
    }

    MyService.java:

    package com.example.learnbroadcastreceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class MyReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            //1.1======================================
            System.out.println("接收到了消息,消息的内容是:" + intent.getStringExtra("data"));
            //=========================================
        }
    }


    02-动态注册和注销 BroadcastReceiver:

  • 相关阅读:
    朴素贝叶斯分类算法原理分析与代码实现
    决策树分类算法原理分析与代码实现
    Eclipse Java 调试基本技巧
    Eclipse Java 开发平台实用技巧
    泛型算法
    集合类型的使用示例
    异常
    内部类
    对象复制
    界面设计常用CSS属性
  • 原文地址:https://www.cnblogs.com/juham/p/15222251.html
Copyright © 2011-2022 走看看