com.squareup.leakcanary:leakcanary-android:1.4-beta2
dependencies {
compile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
}
public class MainApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
RefWatcher refWatcher = LeakCanary.install(this);
}
}
<application
android:allowBackup="true"
android:name=".MainApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
... ...
</application>
public class MyInstance {
private Context context;
private static MyInstance mInstance;
public static MyInstance getInstance(Context context) {
if (mInstance == null) {
synchronized (MyInstance.class) {
if (mInstance == null)
mInstance = new MyInstance(context);
}
}
return mInstance;
}
private MyInstance(Context context) {
this.context = context;
}
}
接着我们新建一个Activity,在onCreate中传入自身的context来初始化MyInstance。
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
MyInstance.getInstance(this);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。