Broadcast两种注册方法

第一种在代码中使用registerReceiver注册

第二种在清单文件androidmanifest中注册(常驻Receiver)

 

private TextView textview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview = (TextView)this.findViewById(R.id.textview);
        //注册自定义BroadcastReceiver(1)
        this.registerReceiver(mBroadcastReceiver, new IntentFilter("com.xxx.action.MYRECEIVER"));
        Intent intent = new Intent("com.xxx.action.MYRECEIVER");
        this.sendBroadcast(intent);
    }
   
    //注册自定义BroadcastReceiver(2)
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){

                @Override
                public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if(action.equals("com.xxx.action.MYRECEIVER")){
                                textview.setText("com.xxx.action.MYRECEIVER");
                        }
                       
                }
           
    };

 

在onCreate中注册自定义(1)mBroadcastReceiver,并用Intent封装消息,最后sendBroadcast发送广播,然后程序进入到BroadcastReceiver 的定义(2)中,在重载的onReceicer这里会处理广播

 

第二种:

Androidmanifest文件中添加:

 

<receiver android:name=".InfoReceiver">
   <intent-filter>
    <action android:name="com.mobimtech.action.BROADCAST"/>
   </intent-filter>
  </receiver>

 

编写继承broadcast类:

package com.mobimtech.message;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class InfoReceiver extends BroadcastReceiver {

 private static final int REQUEST=1;
 private static final int REPLY=2;
 private static final int MESSAGE=3;
 private static final int REPLY_MESSAGE=4;
 
 private Context mContext;
 private String mValue;
 private String mTemp[];
 
 @Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub

  
  mContext=context;
  mValue=intent.getStringExtra("key");
  Log.e("receiver", "............"+mValue);
  mTemp=mValue.split("#");
  show(mTemp[0]);
//  请求指令格式:指令代号+发送方+接收方+附加消息
//  恢复指令消息:指令代号+发送方+接收方+yes/no
 }
 
//显示消息
 public void show(String id){
  if(id.equals("request")){
   
   NotificationManager manager=(NotificationManager)mContext.getSystemService
   (android.content.Context.NOTIFICATION_SERVICE);
   Intent intent=new Intent();
   intent.setClass(mContext,Request.class);
   intent.putExtra("key",mValue);
   PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
   Notification notification=new Notification();
   notification.icon=R.drawable.notification;
   notification.tickerText="新消息";
   notification.setLatestEventInfo(mContext,mTemp[1],"请求加你为好友",appIntent);
   manager.notify(REQUEST, notification);
   Log.e("key","...............");
  }else if(id.equals("reply")){
   
   NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
   Intent intent=new Intent();
   intent.setClass(mContext, Reply.class);
   intent.putExtra("key",mValue);
   PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
   Notification notification=new Notification();
   notification.icon=R.drawable.notification;
   notification.tickerText="新消息";
   if(mTemp[3].equals("refuse")){
    notification.setLatestEventInfo(mContext,mTemp[1],"拒绝添加你为好友",appIntent);
   }else{
    notification.setLatestEventInfo(mContext,mTemp[1],"同意添加你为好友",appIntent);
   }
   manager.notify(REPLY, notification);
  }else if(id.equals("message")){
   
   NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
   Intent intent=new Intent();
   intent.setClass(mContext, ReplyMsg.class);
   intent.putExtra("key",mValue);
   PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
   Notification notification=new Notification();
   notification.icon=R.drawable.notification;
   notification.tickerText="新消息";
   notification.setLatestEventInfo(mContext,mTemp[1],"给你发了短信",appIntent);
   manager.notify(MESSAGE, notification);
   Log.e("Notification","Notification");
  }else if(id.equals("replymessage")){
   NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
   Intent intent=new Intent();
   intent.setClass(mContext, ReplyMsg.class);
   intent.putExtra("key",mValue);
   PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
   Notification notification=new Notification();
   notification.icon=R.drawable.notification;
   notification.tickerText="新消息";
   notification.setLatestEventInfo(mContext,mTemp[1],"回复你发了短信",appIntent);
   manager.notify(REPLY_MESSAGE, notification);
  }
 }
}

 

代码使用广播:

 

Intent intent=new Intent(ACTION);
    intent.putExtra("key", value);
    sendBroadcast(intent);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值