• 即时聊天APP(六)


    通常我们在接收消息的时候会有声音和震动的提示,因此我也加了代码达到这样的效果,这就要用到EventBus了,当然这里我也用到了自定义的广播,所以首先在Mainfests文件中加入以下代码:

    <receiver
    android:name=".MylocalMessage"
    android:process=":bmobpush" >
    <intent-filter>
        <!-- 接收自定义广播的action -->
        <action android:name="cn.edu.sau.action.MESSAGE" />
    </intent-filter>
    </receiver>  
    

    然后再写一个MessageEvent的Bean类对事件进行封装(这里只用到一个String类型的变量):

    public class MessageEvent {
    public final String message;
    public MessageEvent(String message) {
        this.message = message;
    }
    }  
    

    接下来我们在ImMessageHandler中接收到消息,并调用EventBus的post方法发送事件:

    public class ImMessageHandler extends BmobIMMessageHandler{
    /**
     * 接收在线消息
     * @param messageEvent
     */
    @Override
    public void onMessageReceive(MessageEvent messageEvent) {
        super.onMessageReceive(messageEvent);
        final Msg msg = new Msg(messageEvent.getMessage().getContent(), Msg.TYPE_RECEIVED);
        msg.setSender(MyUser.getUni());
        msg.setReceiver(messageEvent.getFromUserInfo().getUserId());
        int len = msg.getContent().length();
        // 过滤字符串
        String str="=[cn.bmob.newim.event.Message Event@";
        String str1="=[cn.bmob.newim.event.MessageEvent@";
        String str2 = msg.getContent().trim();
        int bl = str2.indexOf(str);
        int bo = str2.indexOf(str1);
        if(bl!=-1 || bo!=-1){
        }else{
            if(!msg.getReceiver().equals(Tips.Receiver))
                if(len > 10){
                    String show = msg.getContent().substring(0,8);
                    //使用EventBus发送通知,主Activity中处理事件
                    EventBus.getDefault().post(new cn.edu.sau.joker.MessageEvent(msg.getReceiver()+":"+show+"......"));
                }else {
                    EventBus.getDefault().post(new cn.edu.sau.joker.MessageEvent(msg.getReceiver()+":"+msg.getContent()));
                }
                //聊天界面添加消息
                Main.add(msg);
        }
    }
    /**
     * 接收离线消息,本程序不做处理
     * @param offlineMessageEvent
     */
    @Override
    public void onOfflineReceive(OfflineMessageEvent offlineMessageEvent) {
        super.onOfflineReceive(offlineMessageEvent);
    }
    }  
    

    之前我在MainActivity中写了onMessageEvent方法(方法名字随便写,但是在方法前面要加@Subscribe注解,并且指定线程模型),用来接收事件并进行处理(发送广播):

    //发送广播
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(cn.edu.sau.joker.MessageEvent event) {
        Intent intent = new Intent("cn.edu.sau.action.MESSAGE");
        intent.putExtra("msg",event.message);
        sendBroadcast(intent);
    }  
    

    然后我们在MylocalMessage(为什么我要起这么个类名,请参考Mainfests中的代码)中接收广播,并发送一个通知,指定铃声和震动事件:

    public class MylocalMessage extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 收到广播时,发送一个通知
        String content = intent.getStringExtra("msg");
        NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification notify = new Notification.Builder(context)
                .setSmallIcon(R.drawable.icon)
                .setContentTitle("您收到一条消息")
                .setContentText(content)
                .build();
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        notify.sound = uri;
        long[] vibrates = { 0, 200, 200, 200 };
        notify.vibrate = vibrates;
        manager.notify(1, notify);
    }
    }  
    

    整个项目大致就是这样,欢迎大家的评论和指正(虽然我也听不进去),最后放源码APK .##

  • 相关阅读:
    Hihocoder-小Hi的烦恼
    Python包下载与离线安装
    Shell输出颜色设置
    MySQL主从配置
    MySQL初始化与用户配置
    [转]常用 GDB 命令中文速览
    搭建github静态博客
    树莓派上手
    vim安装与配置
    数组,看了你就懂了!
  • 原文地址:https://www.cnblogs.com/zqm-sau/p/10331978.html
Copyright © 2020-2023  润新知