IM项目总结

作为一个Android初学者,这个项目算学到不少东西;随便总结总结,供以后参考

1.EventBus 1.0.1的使用;
 例:
    (1) XX1.java 触发EventBus事件;
        RefreshUnReadEvent refreshUnReadEvent = new RefreshUnReadEvent();//RefreshUnReadEvent相当于一个javaBean,可进行参数传递
        refreshUnReadEvent.setFromUserid(resp.getFromUserId());
        EventBus.getDefault().post(refreshUnReadEvent);
    (2) XX2.java 注册
        EventBus.getDefault().register(this, "refreshUnRead", RefreshUnReadEvent.class);
    (3) XX2.java 具体实现(此处的方法名需与注册的“refreshUnRead”保持一致)
        public void refreshUnReadMainThread(RefreshUnReadEvent event) {
            //event.getFromUserid()
        }
        
2.推送通知
    例:
    public void notifyUserMessage(String title, String content, Intent intent)
    {
        remove();
        if (mNotificationManager == null)
        {
            mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        mNotification = new Notification(R.drawable.ic_launcher, content, System.currentTimeMillis());
        mNotification.setLatestEventInfo(mContext, title, content, getPendingIntentForMessage(intent));
        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;//根据Notification.FLAG_AUTO_CANCEL 改变推送提示格式
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);// 发起通知
    }
    
    public PendingIntent getPendingIntentForMessage(Intent intent)
    {
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }

3.广播Broadcast
    例:
    (1) XX1.java
        Intent clearUnreadIntent = new Intent(MTActions.CLEAR_CONVERSATION_UNREAD_COUNT_ACTION);
        clearUnreadIntent.putExtra("conversationType", Constants.DISCUSSION_CONVERSATION);
        clearUnreadIntent.putExtra("conversationId", discussion.getDiscussionId());
        mContext.sendBroadcast(clearUnreadIntent);
    (2) XX2.java
        private BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (MTActions.CLEAR_CONVERSATION_UNREAD_COUNT_ACTION.equals(action)) {
                      //操作
                }
            }
        }
        //注册广播
         private void registerReceivers() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(MTActions.CLEAR_CONVERSATION_UNREAD_COUNT_ACTION);
            registerReceiver(mReceiver, filter);
        }
        //注销广播
        unregisterReceiver(mReceiver);
        
4.左划删除(SwipeMenuListView)
    例:
    //此处只做删除操作,具体其它操作度娘
    (1)
    SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(
                    getApplicationContext());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                    0x3F, 0x25)));
            // set item width
            deleteItem.setWidth(dp2px(70));
            // set a icon
            deleteItem.setIcon(R.drawable.ic_delete);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };
    (2)
        private SwipeMenuListView mListView;
        mListView = (SwipeMenuListView) findViewById(R.id.lv_conversation);
        mListView.setMenuCreator(creator);
        mListView.setAdapter(mConversationListAdapter);
        mListView.setOnItemClickListener(this);
         mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0://左划删除
                        deleteViewList(position);
                        refresh();
                        break;
                }
                return false;
            }
        });

6.无法打包或者部分手机不兼容(超过65536,部分Android系统自动分包)
 (1)build.gradle文件
    defaultConfig{
        multiDexEnabled = true
    }
    dependencies {
        //添加支持multidex的兼容包
        compile 'com.android.support:multidex:1.0.0'
    }
 (2)MTApplication.java文件
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
    
7.定时(最大61秒,从61-11开始10秒内,每1秒执行一次)
    CountDownTimer downTimer = new CountDownTimer(61000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished <= 11000 && millisUntilFinished > 0) {
                record_rl.setVisibility(View.GONE);
                rl_daojishi.setVisibility(View.VISIBLE);
                record_daojishi_text.setText(millisUntilFinished / 1000 + "");
            }
        }

        @Override
        public void onFinish() {
            rl_daojishi.setVisibility(View.GONE);
        }
    };

downTimer.start();//开始

downTimer.cancel();//结束
8.
    //按住说话,提示需动态切换图片
    Subscription subscription;
    int position = 0;
    int image[] = {R.drawable.img_yuyin1, R.drawable.img_yuyin2, R.drawable.img_yuyin3, R.drawable.img_yuyin};

    private void changeMicro() {
        if (subscription != null) {
            return;
        }
        record_img.setImageResource(image[0]);
        subscription = Observable.interval(300, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                if (subscription != null && !subscription.isUnsubscribed()) {
                    record_img.setImageResource(image[position % image.length]);
                    position++;
                }
            }
        });
    }
    //取消图片切换
    private void cancelMicro() {
        if (subscription != null) {
            subscription.unsubscribe();
            subscription = null;
            position = 0;
        }
        record_img.setImageResource(R.drawable.record_cancel);
    }    
    
9.textMessage.setAutoLinkMask(Linkify.ALL);

系统自带的高亮,ALL包括WEB_URLS、EMAIL_ADDRESSES、PHONE_NUMBERS、MAP_ADDRESSES

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值