zoukankan      html  css  js  c++  java
  • 如何将BroadcastReceiver中的信息传到Activity中

    方法:在BroadcastReceiver中定义一个接口,在Activity中定义一个BroadcastReceiver的对象,采用动态注册,在Activity中定义接口中的方法并通过BroadcastReceiver对象调用该方法,具体代码如下:

    自定义BroadcastReceiver:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
    private Handle handle;
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
            String msg=intent.getStringExtra("msg");
            handle.handle(msg);//调用接口中的方法
    
        }
        public interface Handle{        //接口定义
            public void handle(String s);
        };
    
    public void setHandle(Handle handle)
    {
        this.handle=handle;
    }
    }    
    

      Activity:

    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements MyBroadcastReceiver.Handle {
    
    private Button broad=null;
    private MyBroadcastReceiver receiver;//定义广播接收器对象
    private ServiceConnection serviceConnection =new ServiceConnection() {
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            broad=(Button)super.findViewById(R.id.broad);
    
    
            receiver=new MyBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.intent.action.EDIT");
            registerReceiver(receiver,intentFilter);//动态注册
    
            receiver.setHandle(this);
    }
    
      
        broad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setAction("android.intent.action.EDIT");
    
                it.putExtra("msg","广播已接收");
                MainActivity.this.sendBroadcast(it);
            }
        });
        }
    
        @Override
    //实现接口中的方法
        public void handle(String s) {
       
            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        }
        protected void onDestroy() {
                    super.onDestroy();
                    unregisterReceiver(receiver);     //注销广播接收器
                }
     }

    注意一点就是这里要使用动态注册,不能用静态注册,因此不需要去配置AndroidMainifest.xml文件

  • 相关阅读:
    任何优秀的程序员, 都有早逝的风险
    租车App第一次迭代报告
    快租车app——需求分析心得
    结对编程——自动生成数学试卷的系统(javaswing,mysql)by 陈松&刘宇航
    结队编程之——阅读分析队友的代码(C++自动生成数学试卷)
    自动生成不同难度的数学试卷系统,并输出到txt文件中,命名为当前时间(java)
    代码之美——浅谈命名规则与代码优化
    关于防抖和节流
    关于sessionStorage和localstorage的一些记录
    vue应用微信二维码登录的一些记录
  • 原文地址:https://www.cnblogs.com/liuleliu/p/12269730.html
Copyright © 2011-2022 走看看