动态注册广播接收者

【 Android 基础 】 专栏收录该内容
299 篇文章 0 订阅
通过broadcast(广播)的形式

当我们的进度发生变化的时候我们发送一条广播,然后在Activity的注册广播接收器,接收到广播之后更新ProgressBar,代码如下

package com.example.communication;  
<span style="font-family:System;">  
import android.app.Activity;  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.content.IntentFilter;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ProgressBar;  
  
public class MainActivity extends Activity {  
    private ProgressBar mProgressBar;  
    private Intent mIntent;  
    private MsgReceiver msgReceiver;  
      
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        //动态注册广播接收器  
        msgReceiver = new MsgReceiver();  
        IntentFilter intentFilter = new IntentFilter();  
        intentFilter.addAction("com.example.communication.RECEIVER");  
        registerReceiver(msgReceiver, intentFilter);  
          
          
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);  
        Button mButton = (Button) findViewById(R.id.button1);  
        mButton.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                //启动服务  
                mIntent = new Intent("com.example.communication.MSG_ACTION");  
                startService(mIntent);  
            }  
        });  
          
    }  
  
      
    @Override  
    protected void onDestroy() {  
        //停止服务  
        stopService(mIntent);  
        //注销广播  
        unregisterReceiver(msgReceiver);  
        super.onDestroy();  
    }  
  
  
    /** 
     * 广播接收器 
     * @author len 
     * 
     */  
    public class MsgReceiver extends BroadcastReceiver{  
  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            //拿到进度,更新UI  
            int progress = intent.getIntExtra("progress", 0);  
            mProgressBar.setProgress(progress);  
        }  
          
    }  
  
}  

package com.example.communication;  
  
import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;  
  
public class MsgService extends Service {  
    /** 
     * 进度条的最大值 
     */  
    public static final int MAX_PROGRESS = 100;  
    /** 
     * 进度条的进度值 
     */  
    private int progress = 0;  
      
    private Intent intent = new Intent("com.example.communication.RECEIVER");  
      
  
    /** 
     * 模拟下载任务,每秒钟更新一次 
     */  
    public void startDownLoad(){  
        new Thread(new Runnable() {  
              
            @Override  
            public void run() {  
                while(progress < MAX_PROGRESS){  
                    progress += 5;  
                      
                    //发送Action为com.example.communication.RECEIVER的广播  
                    intent.putExtra("progress", progress);  
                    sendBroadcast(intent);  
                      
                    try {  
                        Thread.sleep(1000);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                      
                }  
            }  
        }).start();  
    }  
  
      
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        startDownLoad();  
        return super.onStartCommand(intent, flags, startId);  
    }  
  
  
  
    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
  
  
}

Service向Activity发送消息,可以使用广播,当然Activity要注册相应的接收器。比如Service要向多个Activity发送同样的消息的话,用这种方法就更好


摘取于:文章连接

  • 0
    点赞
  • 1
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值