Android 设置后台静态广播和activity进行通信

一:注册设置后台静态广播

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.TextView;  
  
public class MainActivity extends Activity {  
    private Button mButton;  
    private TextView mTextView;  
    public static String ACTION_INTENT_TEST = "com.demo.test";  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mTextView = (TextView) findViewById(R.id.message_tv);  
        mButton = (Button) findViewById(R.id.send_btn);  
        mButton.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent mIntent = new Intent(ACTION_INTENT_TEST);  
                sendBroadcast(mIntent);  
            }  
        });  
        registerMessageReceiver();  
    }  
    //在销毁时要与广播解绑  
    @Override  
    protected void onDestroy() {  
        unregisterReceiver(mMessageReceiver);  
        super.onDestroy();  
    }  
  
    public MessageReceiver mMessageReceiver;  
    public static String ACTION_INTENT_RECEIVER = "com.demo.receiver";  
  
    /** 
     * 动态注册广播 
     */  
    public void registerMessageReceiver() {  
        mMessageReceiver = new MessageReceiver();  
        IntentFilter filter = new IntentFilter();  
  
        filter.addAction(ACTION_INTENT_RECEIVER);  
        registerReceiver(mMessageReceiver, filter);  
    }  
  
    public class MessageReceiver extends BroadcastReceiver {  
  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            // TODO Auto-generated method stub  
            if (intent.getAction().equals(ACTION_INTENT_RECEIVER)) {  
                mTextView.setText(intent.getStringExtra("message"));  
            }  
        }  
  
    }  
}  

二:自定义广播接收器
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.os.Bundle;  
import android.util.Log;  
 
public class MyBroadCast extends BroadcastReceiver{  
  
    public MyBroadCast()  
    {  
        Log.v("BROADCAST_TAG", "MyBroadCast");  
    }  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        // TODO Auto-generated method stub  
        Log.v("BROADCAST_TAG", "onReceive");  
        Bundle bundle = intent.getExtras();  
        if(intent.getAction().equals(MainActivity.ACTION_INTENT_TEST))  
        {  
            processCustomMessage(context, bundle);  
        }  
    }  
    //send msg to MainActivity  
        private void processCustomMessage(Context context, Bundle bundle) {  
              
                Intent mIntent=new Intent(MainActivity.ACTION_INTENT_RECEIVER);  
                mIntent.putExtra("message", "测试Broadcast与Activity之间的通信");  
                context.sendBroadcast(mIntent);  
                  
        }  
  
}  
三:配置注册静态广播
<receiver android:name=".MyBroadCast">  
           <intent-filter >  
               <action android:name="com.demo.test"/>  
           </intent-filter>  
       </receiver>  



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值