美文网首页
android 常用小知识点 tips (二)

android 常用小知识点 tips (二)

作者: 将行陌路 | 来源:发表于2017-03-29 11:15 被阅读48次

    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<ResolveInfo> 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<Void>() {
                        @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<RunningAppProcessInfo> 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"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        
        <!-- 圆角 -->
        <corners
            android:radius="9dp"
            android:topLeftRadius="2dp"
            android:topRightRadius="2dp"
            android:bottomLeftRadius="2dp"
            android:bottomRightRadius="2dp"/><!-- 设置圆角半径 -->
        
        <!-- 渐变 -->
        <gradient
            android:startColor="@android:color/white"
            android:centerColor="@android:color/black"
            android:endColor="@android:color/black"
            android:useLevel="true"
            android:angle="45"
            android:type="radial"
            android:centerX="0"
            android:centerY="0"
            android:gradientRadius="90"/>
        
        <!-- 间隔 -->
        <padding
            android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp"/><!-- 各方向的间隔 -->
        
        <!-- 大小 -->
        <size
            android:width="50dp"
            android:height="50dp"/><!-- 宽度和高度 -->
        
        <!-- 填充 -->
        <solid
            android:color="@android:color/white"/><!-- 填充的颜色 -->
        
        <!-- 描边 -->
        <stroke
            android:width="2dp"
            android:color="@android:color/black"
            android:dashWidth="1dp"
            android:dashGap="2dp"/>
        
    </shape>
    
    填充:设置填充的颜色
    
    间隔:设置四个方向上的间隔
    
    大小:设置大小
    
    圆角:同时设置五个属性,则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"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#80c0c0c0"/>
        <size android:width="1dp"/>
    </shape>
    
    # 代码
    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
    

    相关文章

      网友评论

          本文标题:android 常用小知识点 tips (二)

          本文链接:https://www.haomeiwen.com/subject/gxseottx.html