字符串按指定字节数截取、隐藏与显示输入软键盘、收起状态栏以及判断服务是否运行功能代码段

1、字符串中指定字节数截取,以及多余部分显示方式

/**
* 获取字节长度
* 
*/
public static int getWordCount(String s) {
s = s.replaceAll("[^\\x00-\\xff]", "**");
int length = s.length();
return length;
}
/**
* 
* Description:长度超过3个汉字(6个字符)字符串,自动截取前三个字,多于的已省略号...标识
*
* @param str  要截取的字符串
* @param n  截取的字节数
* @return
*/
public static String getFormatString(String str,int n)
    {
            //格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节
            String temp = "";
            if(getWordCount(str)<=n)//如果长度比需要的长度n小,返回原字符串
            {
                 return str;
            }
            else{
                    int t=0;
                    char[] q = str.toCharArray();
                    for(int i=0;i<q.length&&t<=n;i++)
                    {
                    if((int)q[i]>=0x4e00 && (int)q[i]<=0x9fa5)//是否汉字
                            {                              
                               t += 2;
                            }
                         else {
                               t++;
                            }
//                     if (t <= n) {//判断如果此时字符数大于要求显示的字符数n,是否取该汉字
                    temp += q[i];
// }                    
                    }
                    return (temp+"...");
            }
    } 
}


2、隐藏与显示输入软键盘

/**
     * 隐藏界面输入软键盘
     * 
     * @param activity
     */
    public static void hideSoftInputFromWindow(final Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) activity
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    View view = activity.getCurrentFocus();
                    if (view != null) {
                        imm.hideSoftInputFromWindow(view.getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        });
    }


    /**
     * 显示界面输入键盘
     * 
     * @param activity
     */
    public static void showSoftInputFromWindow(final Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) activity
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    View view = activity.getCurrentFocus();
                    if (view != null) {
                        LogUtil.d("showSoftInput");
                        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                    }
                }
            }
        });
    }

3、收起状态栏的解决方法

/**
* 收起状态栏
* 
* @param context
*            上下文对象
* @return 成功收起状态栏返回true,否则返回false
*/
public static boolean collapseStatusBar(Context context) {
Object statusbarService = context.getSystemService("statusbar");
if (statusbarService == null) {
return false;
}
try {
Class<?> statusBarManager = Class
.forName("android.app.StatusBarManager");
if (statusBarManager == null) {
return false;
}
Method collapseMethod;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
collapseMethod = statusBarManager.getMethod("collapsePanels");
} else {
collapseMethod = statusBarManager.getMethod("collapse");
}
if (collapseMethod == null) {
return false;
}
collapseMethod.invoke(statusbarService);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}

4、判断服务是否运行功能

/**
     * 用来判断服务是否运行.
     * 
     * @param context
     * @param className
     *            判断的服务名字
     * @return true 在运行 false 不在运行
     */
    public static boolean isServiceRunning(Context mContext, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(Integer.MAX_VALUE);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        String name = "";
        String process = "";
        for (int i = 0; i < serviceList.size(); i++) {
            name = serviceList.get(i).service.getClassName();
            process = serviceList.get(i).process;
            if (name.equals(className) == true) {
                if (process
                        .startsWith(BizConstant.NET_PHONE_ACCOUNT_TYPE + ":")) {
                    isRunning = true;
                    break;
                }
            }
        }
        return isRunning;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值