1、重启app
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
String otherProcessName = context.getPackageName() + ":";
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList = activityManager.getRunningAppProcesses();
if(runningAppProcessInfoList != null)
{
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessInfoList) {
if(runningAppProcessInfo.processName.startsWith(otherProcessName))
{
android.os.Process.killProcess(runningAppProcessInfo.pid);
}
}
}
else
{
activityManager.killBackgroundProcesses(context.getPackageName());
}
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
ComponentName componentName = intent.getComponent();
Intent mainIntent = Intent.makeRestartActivityTask(componentName);
context.startActivity(mainIntent);
System.exit(0);
// Process.killProcess(Process.myPid());
// PackageManager packageManager = context.getPackageManager();
// Intent launchIntent = packageManager.getLaunchIntentForPackage(context.getPackageName());
// launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// int requestCode = (int) SystemClock.uptimeMillis();
// PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, launchIntent,
// PendingIntent.FLAG_CANCEL_CURRENT);
// AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);
System.exit(0);
// Process.killProcess(Process.myPid());
2、数组View隐藏与显示
if(data == null)
{
data = new ArrayList<>();
}
final int count = Math.min(itemHolder.views.size(), data.size());
int i = 0;
for (; i < count; i++) {
View view = itemHolder.views.get(i);
view.setVisibility(View.VISIBLE);
//todo
}
for(; i < itemHolder.views.size(); i++)
{
itemHolder.views.get(i).setVisibility(View.GONE);
}
3、Activity中Handler防OOM,主要时要使用静态内部类。AsyncTask,Thread也要这样处理。 Android开发中常见的5大内存泄漏问题及解决办法
public class MainActivity extends AppCompatActivity {
private MyHandler mHandler = new MyHandler(this);
private static class MyHandler extends Handler {
private WeakReference<Context> reference;
public MyHandler(Context context) {
reference = new WeakReference<>(context);
}
@Override
public void handleMessage(Message msg) {
MainActivity activity = (MainActivity) reference.get();
if(activity != null){
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}
4、生成条码和二维码
public static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int desiredWidth, int desiredHeight) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
result = writer.encode(contents, format, desiredWidth,
desiredHeight, null);
} catch (WriterException e) {
e.printStackTrace();
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
5、根据bitmap设置背景相似颜色 https://gist.github.com/tskulbru/12ab15e81b87a34a4c48
/**
* 根据bitmap设置背景相似颜色
* @param bitmap
* @return
*/
public static int getBitmapAverageColor(Bitmap bitmap) {
if(bitmap == null) return 0;
long redBucket = 0;
long greenBucket = 0;
long blueBucket = 0;
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int pixel : pixels) {
redBucket += Color.red(pixel);
greenBucket += Color.green(pixel);
blueBucket += Color.blue(pixel);
}
int red = (int)(redBucket / pixels.length);
int green = (int)(greenBucket / pixels.length);
int blue = (int)(blueBucket / pixels.length);
// Log.i("aaaaaaaaaaaaa", red + "--" + green + "--" + blue);
int averageColor = Color.rgb(red, green, blue);
return averageColor;
}
6、根据bitmap设置textivew颜色
/**
* 根据bitmap设置textivew颜色
* @param view
* @param x textview在view上的getLeft
* @param y textview在view上的getTop
* @param width textview在view上的getWidth
* @param height textview在view上的getHeight
* @return
*/
public static int getBitmapAverageColor(View view, int x, int y, int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
long redBucket = 0;
long greenBucket = 0;
long blueBucket = 0;
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), x, y, width, height);
for (int pixel : pixels) {
redBucket += Color.red(pixel);
greenBucket += Color.green(pixel);
blueBucket += Color.blue(pixel);
}
int red = (int)(redBucket / pixels.length);
int green = (int)(greenBucket / pixels.length);
int blue = (int)(blueBucket / pixels.length);
//颜色值越小,颜色越深
red = (int)(red * 0.299f);
red = (int)(red * 0.587f);
red = (int)(red * 0.114f);
// Log.i("aaaaaaaaaaaaa", red + "--" + green + "--" + blue);
int averageColor = Color.rgb(red, green, blue);
return averageColor;
}
7、文本空格替换
strContent = strContent.replace((char) 12288, ' ');//全局替换成半角
strContent = strContent.replaceAll("[\\t|\\r|\\n|\\s]+", " ");//连续空格替换成一个空格
strContent = str.replace((char) 12288, ' ');
strContent = str.replaceAll("\\s*", "");//去掉所有空格
8、动态设置不弹出键盘或者弹出键盘
//隐藏键盘 即使在AndroidManifest中设置了android:windowSoftInputMode="stateVisible|adjustResize"也有效
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//显示键盘
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
9、查看Activity是否是透明
private boolean windowIsTranslucent(Context context, Class<?> clazz)
{
boolean windowIsTranslucent = false;
try {
ActivityInfo info = context.getPackageManager().getActivityInfo(new ComponentName(context, clazz), PackageManager.GET_META_DATA);
int theme = info.getThemeResource();
int[] attrsArray = {android.R.attr.windowIsTranslucent};
TypedArray typedArray = context.obtainStyledAttributes(theme, attrsArray);
if(typedArray != null)
{
windowIsTranslucent = typedArray.getBoolean(0, false);
typedArray.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
return windowIsTranslucent;
}