Android Tips(一)

判断设备是否是大屏设备

只需在相应分辨率的value配置文件下写入不同的值,如:
在/values/cofig.xml 文件中写入:

1
<bool name="is_large_tablet">false</bool>

在/values-sw720p/cofig.xml 文件中写入:

1
<bool name="is_large_tablet">true</bool>

然后在方法中调用:

1
2
3
4
5
public static boolean isScreenLarge(Resources res) {

return res.getBoolean(R.bool.is_large_tablet);

}

判断是否横屏

1
2
3
4
public static boolean isScreenLandscape(Context context) {
return context.getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE;
}

判断某个包名的应用是否已安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static boolean haveInstallApp(Context context,String packageName) {
if(TextUtils.isEmpty(packageName)) {
return false;
}
// get packagemanager
PackageManager packageManager =context.getPackageManager();
// 获取所有已安装程序的包信息
List<PackageInfo> info = packageManager.getInstalledPackages(0);
if (info != null) {
for (int i = 0; i < info.size(); i++) {
String installPackageName = info.get(i).packageName;
if(packageName.equals(installPackageName)) {
return true;
}
}
}
return false;
}

获取当前程序可用内存

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 获取当前可用内存,返回数据以字节为单位。
*
* @param context
* 可传入应用程序上下文。
* @return 当前可用内存。
*/
private static long getAvailableMemory(Context context) {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
getActivityManager(context).getMemoryInfo(mi);
return mi.availMem;
}

获取状态栏的高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 用于获取状态栏的高度。
*
* @return 返回状态栏高度的像素值。
*/

private int getStatusBarHeight() {
if (statusBarHeight == 0) {
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object o = c.newInstance();
Field field = c.getField("status_bar_height");
int x = (Integer) field.get(o);
statusBarHeight = getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusBarHeight;
}

获取所有桌面应用程序包名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 获得属于桌面的应用的应用包名称
*
* @return 返回包含所有包名的字符串列表
*/

private List<String> getHomes() {
List<String> names = new ArrayList<String>();
PackageManager packageManager = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo ri : resolveInfo) {
names.add(ri.activityInfo.packageName);
}
return names;
}

获取软键盘按键(Enter go等),并实现自定义功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {  

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判断是否是“GO”键*/
if(actionId == EditorInfo.IME_ACTION_GO){
/*隐藏软键盘*/
InputMethodManager imm = (InputMethodManager) v
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(
v.getApplicationWindowToken(), 0);
}

edittext.setText("success");
webview.loadUrl(URL);

return true;
}
return false;
}
});

---

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionSearch"/>
actionNone : 回车键,按下后光标到下一行
actionGo : Go,
actionSearch : 放大镜
actionSend : Send
actionNext : Next
actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框

获取栈顶Activity

1
2
3
4
5
6
7
   public static String getClassNameOnStackTop(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String className = cn.getClassName();
LogUtil.d("top activity=" + className);
return className;
}

获取屏幕方向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private int getDeviceOrientation(Context context) {
WindowManager windowManager = (WindowManager)
context.getSystemService(Context.WINDOW_SERVICE);
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
boolean isLandscape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE) &&
(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180);
boolean isRotatedPortrait = (config.orientation == Configuration.ORIENTATION_PORTRAIT) &&
(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
if (isLandscape || isRotatedPortrait) {
return CellLayout.LANDSCAPE;
} else {

return CellLayout.PORTRAIT;
}
}

屏蔽Home键

重写onAttachedToWindow

1
2
3
4
5
6
7
8
9
10
11
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_HOME == keyCode) {
Toast.makeText(getApplicationContext(), "按了HOME 键...",Toast.LENGTH_LONG).show();

}
return super.onKeyDown(keyCode, event); //return true则将home键点击事件拦截
}

热评文章