android 两个程序之间如何通过 service 来监听广播

一个用来发送广播同时自己也接受广播,另一个是用service来监听上一个的广播,当上一个发出广播时,给出相应。

一、全局广播:
发送的activity:

import android.widget.Toast;
 
public class TestActivity extends Activity {
    
    private final String ACTION_NAME = "send Message";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView t = (TextView) this.findViewById(R.id.tv_test);
        t.setText("send broadcast");
        registerBroadcastReceiver();
        t.setOnClickListener(new View.OnClickListener() {//发送广播
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(ACTION_NAME);
                mIntent.putExtra("message", "send broadcast");
                sendBroadcast(mIntent);
            }
        });
    }
    
    private BroadcastReceiver  mBroadcastReceiver = new BroadcastReceiver() {//接收广播
        
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action  = intent.getAction();
            if(action.equals(ACTION_NAME)){
                String  tt = intent.getStringExtra("message");
//                Toast.makeText(getApplicationContext(), tt, 0).show();  //取消这行可显示信息
            }
            
        }
    };
    
    private void registerBroadcastReceiver(){
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver,mFilter);
    }
    
    
    @Override
    public void onBackPressed() {//推出取消监听广播
        unregisterReceiver(mBroadcastReceiver);
        super.onBackPressed();
        
    }
    
    
}

service 代码:

import android.widget.Toast;
 
 
 
public class MyService extends Service{
 
    private final String ACTION_NAME = "send Message";//监听广播Intent 的名称。
    
    @Override
    public void onCreate() {
        Toast.makeText(this, "frist create", 0).show();
        registerBroadcastReciver();
        System.out.println("test");
        super.onCreate();
    }
 
    @Override
    public void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);//服 务销毁时,取消监听广播
        super.onDestroy();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerBroadcastReciver();
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {//监听广播
        
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if(intent.getAction().equals(ACTION_NAME)){
                Toast.makeText(getApplicationContext(), "Servicde: "+intent.getStringExtra("message"), 0).show();
            }
            
        }
    };
    
    public void registerBroadcastReciver(){//注册广播
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver, mFilter);
    }
    
}

二、本地广播的使用:
接收的service

public class ButtonService extends Service {
    //region 使用本地广播与service通信
    LocalBroadcastManager localBroadcastManagerBendi;
    private MsgReceiverBendi msgReceiverbendi;
@Override
    public void onCreate() {
        super.onCreate();
        registerBroadcastReciverBendi();
}
@Override
    public void onDestroy() {
        //unregisterReceiver(mBroadcastReceiver);//服务销毁时,取消监听广播
        if (msgReceiverbendi != null) {
            localBroadcastManagerBendi.unregisterReceiver(msgReceiverbendi);
        }
        super.onDestroy();
    }
//广播接收,用于处理接收到的数据
    public class MsgReceiverBendi extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            final String action = intent.getAction();
            if (ConnectService.MQTT_MSG_SHOW_DESKTOP.equals(action)) {
                String ip = intent.getStringExtra(ConnectService.EXTRA_UDP_DATA_IP);
                String message = intent.getStringExtra("message");
                int port = intent.getIntExtra(ConnectService.EXTRA_UDP_DATA_PORT, -1);
                Log.v("hjhdakdhksda", message);
                //Toast.makeText(getApplicationContext(), "Servicde: " + intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();
                recvMsgTemp = intent.getStringExtra("message");

            }
        }
    }

    /*public void registerBroadcastReciver(){//注册广播
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectService.MQTT_MSG_SHOW_DESKTOP);
        registerReceiver(mBroadcastReceiver, mFilter);
    }*/

    public void registerBroadcastReciverBendi(){//注册广播

        localBroadcastManagerBendi = LocalBroadcastManager.getInstance(this);
        msgReceiverbendi = new MsgReceiverBendi();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectService.MQTT_MSG_SHOW_DESKTOP);
        localBroadcastManagerBendi.registerReceiver(msgReceiverbendi, intentFilter);
    }

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerBroadcastReciverBendi();
        return super.onStartCommand(intent, flags, startId);
    }

发送端方法:

LocalBroadcastManager localBroadcastManager;

try {
            localBroadcastManager = LocalBroadcastManager.getInstance(this);
        } catch (Exception e) {
            e.printStackTrace();            
        }

发送:

broadcastUpdate(ConnectService.MQTT_MSG_SHOW_DESKTOP, "卧室温度:" + tempStr + " °C");


void broadcastUpdate(String action, String message) {
        final Intent intent = new Intent(action);
        intent.putExtra("message", message);
        localBroadcastManager.sendBroadcast(intent);
    }


请为这篇文章评分:
( 已有 1 人评分, 平均得分: 5 分 )

标签: Android 本地通知 通知

发表评论: