美文网首页
Android开发小结1

Android开发小结1

作者: 陽_young | 来源:发表于2016-06-23 20:10 被阅读37次

    接触Android约有半年时间了,虽然有两年的java开发经验,发现Android还是有很多需要学习的地方。有些知识点是第一次用到,简单记录下来,一方面加深印象,一方面供以后参考。

    1.EventBus 1.0.1的使用

    例:

    (1) XX1.java 触发EventBus事件;

    RefreshUnReadEvent refreshUnReadEvent = new RefreshUnReadEvent();
    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()
    }
    

    此处RefreshUnReadEvent 相当于传统的javaBean,可利用其进行传参。

    2.推送通知

    例:

    //设置点击 推送信息跳转页面
    Intent intent = new Intent();intent.setClass(mContext, HomeActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Bundle bundle = new 
    bundle.putString("fileId", message.getFromUserFileid());
    bundle.putBoolean("isGroup", false);intent.putExtras(bundle);//固定显示推送消息 为“您有一条新的消息” liuyangNotificationUtil.getInstance(mContext).notifyUserMessage("导购顾问", "您有一条新的消息", intent);
    //具体推送操作
    public void notifyUserMessage(String title, String content, Intent intent)
        {
            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)

    例:

    //具体见github 有现成demo
    //此处只做删除操作,具体其它操作度娘
        (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;
                }
            });
    

    5.无法打包或者部分手机不兼容(超过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);
        }
    
    

    6.定时(最大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);
        }
    };
    

    .

    7.

        //按住说话,提示需动态切换图片 liuyang
        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);
        }   
    

    8.高亮,链接

    textMessage.setAutoLinkMask(Linkify.ALL);
    

    9.Shader(盗用同事的文章)

    [简书]http://www.jianshu.com/p/08cf25f51c73

    10.ActionSheet盗用同事的文章)

    [简书http://www.jianshu.com/p/1b548491bd5a]

    相关文章

      网友评论

          本文标题:Android开发小结1

          本文链接:https://www.haomeiwen.com/subject/jxfodttx.html