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:

  • 相关阅读:
    实现treeview的动态加载
    sql server2000中使用convert来取得datetime数据类型样式(全)
    一道微软公司的面试题目的算法实现
    后台一行输入太多内容,使前台显示自动换行方法
    在js中刷新本页
    关于datediff函数的用法
    C#中StringBuilder类的使用(转)
    在字符串中使用引号("")等字符 需要用转义字符\ 例如
    常用的SQL和TSQL语句(一) (转)
    JS弹出窗口的运用与技巧(转)
  • 原文地址:https://www.cnblogs.com/juham/p/15222251.html
Copyright © 2011-2022 走看看