您的位置:首页 > 编程语言

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

2014-12-11 10:01 302 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐