zoukankan      html  css  js  c++  java
  • Android 珍藏(三)

    1、Android判断是Pad或者手机

        public boolean isTabletDevice() {  
                TelephonyManager telephony = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);  
                int type = telephony.getPhoneType();  
                if (type == TelephonyManager.PHONE_TYPE_NONE) {  
                    return true;  
                } else {  
                    return false;  
                }  
            }  

    2、到android设置中的卸载界面

    Uri uri = Uri.fromParts("package", appInfo.packageName, null);       
    Intent it=new Intent();  
    it.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");  
    it.setData(uri);  
    startActivity(it); 

    3、获取屏幕上正在显示的Activity

    ActivityManager mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    ComponentName mComponentName = mActivityManager.getRunningTasks(1).get(0).topActivity; 

    4、如何判断一个activity是否存在于系统中

        Intent intent = new Intent();  
        intent.setClassName("包名", "类名");        
        if(getPackageManager().resolveActivity(intent, 0) == null) {  
            //说明系统中不存在这个activity  
        }  

    5、如何获取状态栏和标题栏的高度

    获取状态栏高度:
    
    decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个 getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。于是,我们就可以算出状态栏的高度。
    
    Rect frame = new Rect();  
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
    int statusBarHeight = frame.top; 
    
    获取标题栏高度: 
    
    getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。 
    
        int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
        //statusBarHeight是上面所求的状态栏的高度  
        int titleBarHeight = contentTop - statusBarHeight;  

    6、如何判断快捷方式是否已经创建

     快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中 
    
     boolean isInstallShortcut = false ;  
        final ContentResolver cr = context.getContentResolver();  
        final String AUTHORITY = "com.android.launcher.settings";  
        final Uri CONTENT_URI = Uri.parse("content://" +  
                         AUTHORITY + "/favorites?notify=true");  
          
        Cursor c = cr.query(CONTENT_URI,  
        new String[] {"title","iconResource" },  
        "title=?",  
        new String[] {"XXX" }, null);//XXX表示应用名称。  
                if(c!=null && c.getCount()>0){  
            isInstallShortcut = true ;  
        }  
        /*try { 
            while (c.moveToNext()) { 
                                        String tmp = ""; 
                tmp = c.getString(0); 
            } 
            } catch (Exception e) { 
     
            } finally { 
                c.close(); 
            }*/  
        return isInstallShortcut ;  
    }  

    要有权限:
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
    注意:2.2及其之后的版本不能用这个方法判断!(虽然在launcher.db数据库里还有favorites这个表)

    7、如何在android的一个应用中调用另外一个应用

    Intent intent = new Intent();  
    //第一个参数另一个应用的包名,第二个是需要启动的类  
    intent.setComponent(new ComponentName("com.Ex03_03","com.Ex03_03.Ex03_03"));  
    startActivity(intent);

    8、如何监测是否静音

        AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);   
        switch (am.getRingerMode()) {   
            case AudioManager.RINGER_MODE_SILENT:   
                Log.i("MyApp","Silent mode");   
                break;   
            case AudioManager.RINGER_MODE_VIBRATE:   
                Log.i("MyApp","Vibrate mode");   
                break;   
            case AudioManager.RINGER_MODE_NORMAL:   
                Log.i("MyApp","Normal mode");   
                break;   
        }   
  • 相关阅读:
    JAVA查询树结构数据(省市区)使用hutool工具实现
    定时器
    工作队列
    中断类型
    通过风扇FG脚检测风扇转速
    共享中断
    Linux中断信号的查看
    使用Alibaba Cloud Linux 2系统开突发型实例遇到宿主机一直超分案例
    React学习(三)----- 组件的生命周期
    React学习(二)----- 面向组件编程
  • 原文地址:https://www.cnblogs.com/weizilong/p/3276779.html
Copyright © 2011-2022 走看看