android 常用小知识点 tips (二)
16lz
2021-01-23
android 常用小知识点 tips (一)
android 常用小知识点 tips (二)
持续更新中....
[TOC]
11、根据包名检测应用是否被安装
public static boolean isIntalled(Context context, String packageName) { boolean exist = false; PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List resolveInfoList = pm.queryIntentActivities(intent, 0); for (ResolveInfo resolveInfo : resolveInfoList) { if (resolveInfo.activityInfo.packageName.equals(packageName)) { exist = true; } } return exist; }
12、RxJava防抖动
//引入依赖 compile 'com.jakewharton.rxbinding:rxbinding:0.4.0' compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0' compile 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'/** *实现 */RxView.clicks(view.findViewById(R.id.weixin)) .throttleFirst(4, TimeUnit.SECONDS) .subscribe(new Action1() { @Override public void call(Void aVoid) { ((LoginAndRegisterActivity) activity).doWXClick(); } });
13、判断android 应用是否在前台运行
/** * 程序是否在前台运行 * * @return */ public boolean isAppOnForeground() { // Returns a list of application processes that are running on the // device ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); String packageName = getApplicationContext().getPackageName(); List appProcesses = activityManager .getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; } @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume: "); Log.i(TAG, "onResume2: "+isAppOnForeground(this)); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause: "); Log.i(TAG, "onPause2: "+isAppOnForeground(this));//true 所以进入后台时不要在onPause()方法监听,应该在onStop()里 } @Override protected void onStop() { super.onStop(); Log.i(TAG, "onStop: "); Log.i(TAG, "onStop2: "+isAppOnForeground(this));//false }
14、java引用正则表达式
public static void main(String[] args) { // 要验证的字符串 String str = "service@xsoftlab.net"; // 邮箱验证规则 String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\.){1,3}[a-zA-z\-]{1,}"; // 编译正则表达式 Pattern pattern = Pattern.compile(regEx); // 忽略大小写的写法 // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); // 字符串是否与正则表达式相匹配 boolean rs = matcher.matches(); System.out.println(rs); }
15、android shape详解
<?xml version="1.0" encoding="utf-8"?> 填充:设置填充的颜色间隔:设置四个方向上的间隔大小:设置大小圆角:同时设置五个属性,则Radius属性无效android:Radius="20dp" 设置四个角的半径android:topLeftRadius="20dp" 设置左上角的半径 android:topRightRadius="20dp" 设置右上角的半径 android:bottomLeftRadius="20dp" 设置右下角的半径 android:bottomRightRadius="20dp" 设置左下角的半径描边:dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框android:width="20dp" 设置边边的宽度 android:color="@android:color/black" 设置边边的颜色 android:dashWidth="2dp" 设置虚线的宽度 android:dashGap="20dp" 设置虚线的间隔宽度渐变:当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错。android:useLevel 这个属性不知道有什么用。
16、android倒计时TextView
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); handler.postDelayed(runnable, 1000); } Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { recLen++; txtView.setText("" + recLen); handler.postDelayed(this, 1000); } }; }
17、查看程序占用端口命令
打开cmd,输入
Windows:
netstat -ano | find "8081"
Linux:
netstat -ano | grep 8081
关闭这个进程 命令:tskill 'xxxx' 或者任务管理器中找到对应PID 结束任务
18、android 清空任务栈
Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.setClass(MainActivity.this, Activity2.class); startActivity(intent);FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK ,这两个Flag结合才能实现。
19、TabLayout添加分割线
#分割线shape<?xml version="1.0" encoding="utf-8"?> # 代码LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0); linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); linearLayout.setDividerPadding(15); linearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.layout_divider_vertical));
20、华为手机logcat输出
华为手机设置日志输出:拨号输入*#*#2846579#*#*,进入projectmenu--后台设置--LOG设置--LOG开关--打开;
21、RN Android Monitor调起调试面板
adb shell input keyevent 82
22、synchronized 用法
synchronized修饰非静态方法、同步代码块的synchronized (this)用法和synchronized (非this对象)的用法锁的是对象,线程想要执行对应同步代码,需要获得对象锁。synchronized修饰静态方法以及同步代码块的synchronized (类.class)用法锁的是类,线程想要执行对应同步代码,需要获得类锁。
更多相关文章
- Android布局文件中控件的高度与宽度属性设置
- TextView——行间距与字母间隔
- android默认字体大小、高度、宽度
- 获取Android 手机屏幕宽度和高度以及获取Android手机序列号
- Android获取屏幕宽度的4种方法
- 如何让listview宽度自适应