标签:系统 维护 com 搜索功能 lin play lint except 单例
<application
android:name=".MyApplication"
...
x
<application
android:name=".MyApplication"
...
按我的理解就是,当APP处于前台时,但是所有后台程序都被kill光了,但是还是内存不足时,系统就会调用这个方法告诉APP:兄弟轮到你了。我们可以在这个方法里面释放一些不重要的资源,来保证到时候内存足够而让APP进程不被系统杀掉,或者提醒用户清一下垃圾,让内存清一点空位出来,我的手机老是这样提示我,不知道是不是这个方法惹的祸。
x
按我的理解就是,当APP处于前台时,但是所有后台程序都被kill光了,但是还是内存不足时,系统就会调用这个方法告诉APP:兄弟轮到你了。我们可以在这个方法里面释放一些不重要的资源,来保证到时候内存足够而让APP进程不被系统杀掉,或者提醒用户清一下垃圾,让内存清一点空位出来,我的手机老是这样提示我,不知道是不是这个方法惹的祸。
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
Log.i("bqt", "当前Activity" + "(" + activity.getClass().getSimpleName()+".java" + ":" +30 + ")");
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
});
x
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
public void onActivityDestroyed(Activity activity) {
}
public void onActivityStarted(Activity activity) {
}
public void onActivityResumed(Activity activity) {
Log.i("bqt", "当前Activity" + "(" + activity.getClass().getSimpleName()+".java" + ":" +30 + ")");
}
public void onActivityPaused(Activity activity) {
}
public void onActivityStopped(Activity activity) {
}
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
});
registerComponentCallbacks(new ComponentCallbacks2() {
@Override
public void onTrimMemory(int level) { //ComponentCallbacks2 比 ComponentCallbacks 多了这个回调方法
Log.i("bqt", "【onTrimMemory】" + level);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i("bqt", "【onConfigurationChanged】");
}
@Override
public void onLowMemory() {
Log.i("bqt", "【onLowMemory】");
}
});
registerComponentCallbacks(new ComponentCallbacks2() {
public void onTrimMemory(int level) { //ComponentCallbacks2 比 ComponentCallbacks 多了这个回调方法
Log.i("bqt", "【onTrimMemory】" + level);
}
public void onConfigurationChanged(Configuration newConfig) {
Log.i("bqt", "【onConfigurationChanged】");
}
public void onLowMemory() {
Log.i("bqt", "【onLowMemory】");
}
});
public class App {
private static Application application;
private App() {
}
//通过反射方式获取当前应用的Application
@SuppressLint("PrivateApi")
public static Application get() {
if (application == null) {
try {
Class<?> clazz = Class.forName("android.app.ActivityThread");
Field field = clazz.getDeclaredField("sCurrentActivityThread");
field.setAccessible(true);
Object object = field.get(null);//得到ActivityThread的对象,虽然是隐藏的,但已经指向了内存的堆地址
Method method = clazz.getDeclaredMethod("getApplication");
method.setAccessible(true);
application = (Application) method.invoke(object);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//没有自定义时为【android.app.Application】,自定义时为你设置的Application
Log.i("bqt", "当前应用的Application为:" + application.getClass().getName());
return application;
}
}
x
public class App {
private static Application application;
private App() {
}
//通过反射方式获取当前应用的Application
("PrivateApi")
public static Application get() {
if (application == null) {
try {
Class<?> clazz = Class.forName("android.app.ActivityThread");
Field field = clazz.getDeclaredField("sCurrentActivityThread");
field.setAccessible(true);
Object object = field.get(null);//得到ActivityThread的对象,虽然是隐藏的,但已经指向了内存的堆地址
Method method = clazz.getDeclaredMethod("getApplication");
method.setAccessible(true);
application = (Application) method.invoke(object);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
//没有自定义时为【android.app.Application】,自定义时为你设置的Application
Log.i("bqt", "当前应用的Application为:" + application.getClass().getName());
return application;
}
}
public class MyApplication extends Application {
public static MyApplication application;
@Override
public void onCreate() {
super.onCreate();
application = this;
}
}
public class MyApplication extends Application {
public static MyApplication application;
public void onCreate() {
super.onCreate();
application = this;
}
}
Log.i("bqt", "" + (App.get() == MyApplication.application));//true,完全是同一个对象,使用时完全不用担心有bug
Log.i("bqt", "" + (App.get() == MyApplication.application));//true,完全是同一个对象,使用时完全不用担心有bug
标签:系统 维护 com 搜索功能 lin play lint except 单例
原文地址:https://www.cnblogs.com/baiqiantao/p/9610018.html