内部类Handler造成内存泄露

改成这样就行了

..
public class BaseActivity extends AppCompatActivity {
    protected MyHandler mHandler;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       ..
       mHandler = new MyHandler(this);
       ..     
   }
    
    /**
     * 内部类使用Handler造成内存泄露
     */
    protected static class MyHandler extends Handler {
        private WeakReference<BaseActivity> mActivity;
    
        private MyHandler(BaseActivity baseActivity) {
            mActivity = new WeakReference<>(baseActivity);
        }
    
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (mActivity.get() == null) {
                return;
            }
            mActivity.get().myHandleMessage(msg);
        }
    }

    protected void myHandleMessage(Message msg) {
    }    
}

垂直翻转

要实现ImageView绕中心垂直翻转

final ObjectAnimator oa1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0f);
final ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "scaleX", 0f, 1f);
oa1.setInterpolator(new DecelerateInterpolator());
oa2.setInterpolator(new AccelerateDecelerateInterpolator());
oa1.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        imageView.setImageResource(R.drawable.frontSide);
        oa2.start();
    }
});
oa1.start();

底部阴影

设置elevation属性不支持5.0以下,要想兼容

新建一个drawable,bottom_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@android:color/transparent"
        android:endColor="#33000000"
        android:angle="90">
    </gradient>
</shape>

然后在需要加阴影的控件底部加上一个view,和加一条线一样

<ConstraintLayout>
    ..
    <TextView
        android:id="@+id/tv_need_shadow" .. />
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@drawable/bottom_shadow.xml"
        app:layout_constraintTop_toBottomOf="@id/tv_need_shadow" />
    ..
</ConstraintLayout>

前台服务

先建立一个文件ForegroundService.class,继承Service类

public class ForegroundService extends Service {
    ..
}

算了懒得写了,直接Copy改改就能用了

- 阅读全文 -

常用通讯软件包名

public static final String QQ = "com.tencent.mobileqq";// QQ
public static final String WECHAT = "com.tencent.mm";// 微信
public static final String MMS = "com.android.mms";// 短信
public static final String CALL = "com.android.incallui";// 电话
public static final String FACEBOOK = "com.facebook.katana";// Facebook
public static final String TWITTER = "com.twitter.android";// 推特
public static final String LINKEDIN = "com.linkedin.android";
public static final String INSTAGRAM = "com.instagram.android";
public static final String PINTEREST = "com.pinterest";
public static final String WHATSAPP = "com.whatsapp";
public static final String LINE = "jp.naver.line.android";
public static final String FACEBOOK_MESSENGER = "com.facebook.orca";
public static final String KAKAO = "com.kakao.talk";
public static final String SKYPE = "com.skype.raider";
public static final String EMAIL = "com.android.email";
public static final String TELEGRAM = "org.telegram.messenger"; 
public static final String VIBER = "com.viber.voip";
public static final String CALENDAR = "com.android.calendar";

集成Bugly

先去bugly官网,整一个新应用
https://bugly.qq.com/v2/workbench/apps

然后会生成一个ID,类似这样的

2ca4891a72

在build.gradle里添加上依赖。版本号自己整最新的

dependencies {
    ..
    implementation 'com.tencent.bugly:crashreport:3.1.0'// bugly
    ..
}

在BaseApplication里,写上一行代码。记得把ID替换成自己的

@Override
public void onCreate() {
    ..
    CrashReport.initCrashReport(getApplicationContext(), "2ca4891a72", false);
    ..
}

写一个闪退bug
去bugly上看有没有成功捕获

另外,如果你有用户ID,可以设置进去,在后台就能精准定位到是哪个用户闪退了

String userId = SP.getInstance().getUserId();
CrashReport.setUserId(userId);

分包

有模块方法数超过65536会出现无法编译的情况:

Cannot fit requested classes in a single dex file (# fields: 65599 > 65536)

分包即可解决。

在BaseApplication所在的模块下,build.grade里,引入包:

dependencies {
..
implementation 'com.android.support:multidex'
..  
}

然后在方法数超出的模块下,build.gradle里,添加上配置:

android {
    ..
    defaultConfig {
        multiDexEnabled true
    }
    ..
}

让BaseApplication继承于MultiDexApplication

public class BaseApplication extends MultiDexApplication {
    ..
}

如果没有BaseApplication,就先整一个BaseApplication再进行上面操作