Android过时方法替代

看见有人推荐了Alibaba Java Coding Guidelines
就安装了一下这个插件,发现好多问题啊。
很多时候copy前辈们的代码,有很多都过时了,
下面总结一下过时的替代,提醒自己纠正错误写法

getColor()

ContextCompat.getColor(context, R.color.my_color)

getDrawable()

ContextCompat.getDrawable(context, R.color.my_color)

setBackgroundDrawable()

view.setBackgroundResource(R.drawable.status_question);

android.text.ClipboardManager

android.content.ClipboardManager替代,
同样被废弃还有setText/getText/hasText方法,
使用setPrimaryClip/getPrimaryClip/hasPrimaryClip

Build.VERSION.SDK

Build.VERSION.SDK_INT

getWidth()和getHeight()

Point size = new Point(); 
activity.getWindowManager().getDefaultDisplay().getSize(size); 
int width = size.x; 
int height = size.y;

mViewPager.setOnPageChangeListener(this);

addOnPageChangeListener

getAllNetworkInfo()

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Network[] networks = connectivity.getAllNetworks();
        NetworkInfo networkInfo;
        for (Network mNetwork : networks) {
            networkInfo = connectivity.getNetworkInfo(mNetwork);
            if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                return true;
            }
        }
    } else {
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }

        }
    }

    return false;
}}

onAttach

 public void onAttach(Context context)

android.text.ClipboardManager

android.content.ClipboardManager;
cmb.setText(content.trim());
cmb.setPrimaryClip(ClipData.newPlainText(null, content.trim()));
cmb.getText();
cmb.getPrimaryClip().getItemAt(0).getText();

getColorStateList(resId)

ContextCompat.getColorStateList(getContext(), resId);

你可能感兴趣的:(Android过时方法替代)