Android開發小結1

接觸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.lxweimin.com/p/08cf25f51c73

10.ActionSheet盜用同事的文章)

[簡書http://www.lxweimin.com/p/1b548491bd5a]

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容