1. 安装apk:
public static void installApk(Context context, String apkPath) { File apkfile = new File(apkPath); if (!apkfile.exists()) { return; } Intent i = new Intent(android.content.Intent.ACTION_VIEW); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); context.startActivity(i); }
2. 设置ContextMenu标题颜色:
<item name="android:alertDialogTheme">@style/alert_dialog_theme</item> <style name="alert_dialog_theme" parent="@android:style/Theme.Holo.Light.Dialog"> <item name="android:windowTitleStyle">@style/DialogWindowTitle</item> </style> <style name="DialogWindowTitle"> <item name="android:maxLines">1</item> <item name="android:scrollHorizontally">true</item> <item name="android:textAppearance">@style/MyTextAppearance</item> </style> <style name="MyTextAppearance"> <item name="android:textSize">22sp</item> <item name="android:textColor">@android:color/holo_red_light</item> </style>
3. 拷贝文件:
public static void copyFile(File src, File dst) throws IOException { FileChannel in = new FileInputStream(src).getChannel(); FileChannel out = new FileOutputStream(dst).getChannel(); try { in.transferTo(0, in.size(), out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }
public static boolean networkIsAvailable(Context ctx) { ConnectivityManager cManager = (ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cManager.getActiveNetworkInfo(); return info != null && info.isAvailable() && info.isConnected(); }
5. 检测GPS是否打开:
public static boolean gpsIsOpend(Context ctx) { LocationManager lm = (LocationManager) ctx .getSystemService(Context.LOCATION_SERVICE); return lm .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER); }
6. TextView跑马灯效果:
android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true"
以上属性缺一不可或在Java代码中设置对应属性值。
7. 进入应用自动弹出软键盘:
@Override protected void onResume() { // TODO Auto-generated method stub new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mEditText.requestFocus(); showInputMethod(mEditText); } }, 100); super.onResume(); } private void showInputMethod(View view) { if (view instanceof EditText) { InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(view, 0); } }
8. 4.0之后隐藏 虚拟键:
public class MainActivity extends Activity implements OnClickListener { private View mMainView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMainView = findViewById(R.id.main); mMainView.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub int i = mMainView.getSystemUiVisibility(); if (i == View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) { mMainView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } else if (i == View.SYSTEM_UI_FLAG_VISIBLE) { mMainView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); } else if (i == View.SYSTEM_UI_FLAG_LOW_PROFILE) { mMainView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); } } }
9. 获得app版本:
// 获得 AndroidManifest.xml android:versionName 字段 public static String getVersionName(Context ctx) { PackageManager pManager = ctx.getPackageManager(); String version = null; try { version = pManager.getPackageInfo(ctx.getPackageName(), 0).versionName; } catch (NameNotFoundException e) { // TODO: handle exception e.printStackTrace(); } finally { return version; } } // 获得 AndroidManifest.xml android:versionCode 字段 public static int getVersionCode(Context ctx) { PackageManager pManager = ctx.getPackageManager(); int versionCode = 0; try { versionCode = pManager.getPackageInfo(ctx.getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { // TODO: handle exception e.printStackTrace(); } finally { return versionCode; } }
10. dip与px互转:
public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
11. Bitmap与字节流互转:
public byte[] serialize(Bitmap bitmap) { if (bitmap == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 将bitmap压缩成PNG编码,质量为100%存储 bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } public Bitmap unserialize(byte[] bizdata) { return BitmapFactory.decodeByteArray(bizdata, 0, bizdata.length, null); }
12. 跳到指定联系人收件箱:
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("sms", number, null)));
13.Ubuntu(Linux)下下载的mp3文件名乱码:
写个脚本: #!/bin/bash convmv -r -f utf8 -t iso88591 $1 --notest --nosmart && convmv -r -f gbk -t utf8 $1 --notest --nosmart # $1 第一个参数---一个目录