1、创建快捷方式:
//添加权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
public void installShortCut(String name, int icon, Intent intent) {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
shortcut.putExtra("duplicate", false); // 不允许重复创建
// 快捷方式的图标
ShortcutIconResource iconRes = ShortcutIconResource.fromContext(mContext, icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
intent.setAction("android.intent.action.MAIN");// 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
intent.addCategory("android.intent.category.LAUNCHER");// 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
mContext.sendBroadcast(shortcut);
}
2、删除快捷方式:
//权限
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
public static void deleteShortcut(Context context , String shortcutName ,
Intent actionIntent , boolean isDuplicate) {
Intent shortcutIntent = new Intent ("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME ,shortcutName);
shortcutIntent.putExtra("duplicate" , isDuplicate);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT , actionIntent);
context.sendBroadcast(shortcutIntent);
}
3、判断快捷方式的存在与否:
public String getLauncherPackageName(Context context) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo res = context.getPackageManager()
.resolveActivity(intent, 0);
if (res.activityInfo == null) {
return "";
}
if (res.activityInfo.packageName.equals("android")) {
return "";
} else {
return res.activityInfo.packageName;
}
}
public boolean hasShortcut(String name) {
String url;
String packageName = getLauncherPackageName(mContext);
if (packageName == null || packageName.equals("")
|| packageName.equals("com.android.launcher")) {
int sdkInt = android.os.Build.VERSION.SDK_INT;
if (sdkInt < 8) { // Android 2.1.x(API 7)以及以下的
packageName = "com.android.launcher.settings";
} else if (sdkInt < 19) {// Android 4.4以下
packageName = "com.android.launcher2.settings";
} else {// 4.4以及以上
packageName = "com.android.launcher3.settings";
}
}
url = "content://" + packageName + ".settings/favorites?notify=true";
try {
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), new String[] {
"title", "iconResource" }, "title=?",
new String[] { name }, null);
if (cursor != null && cursor.getCount() > 0) {
return true;
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return false;
}
4、显示或隐藏虚拟键盘:
显示:
InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
隐藏:
InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
imm.hideSoftInputFromWindow(m_edit.getWindowToken(), 0);
5、从assets中读取文本和图片资源:
/** 从assets 文件夹中读取文本数据 */
public static String getTextFromAssets(final Context context, String fileName) {
String result = "";
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, "UTF-8");
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** 从assets 文件夹中读取图片 */
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}
6、进行px和dp之间的转换:
import android.content.Context;
public class DensityUtil {
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
7、检查有没有应用程序来接受处理你发出的intent:
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
8、获取网络类型名称:
public static String getNetworkTypeName(Context context) {
if (context != null) {
ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectMgr != null) {
NetworkInfo info = connectMgr.getActiveNetworkInfo();
if (info != null) {
switch (info.getType()) {
case ConnectivityManager.TYPE_WIFI:
return "WIFI";
case ConnectivityManager.TYPE_MOBILE:
return getNetworkTypeName(info.getSubtype());
}
}
}
}
return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);
}
public static String getNetworkTypeName(int type) {
switch (type) {
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "CDMA - EvDo rev. 0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "CDMA - EvDo rev. A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "CDMA - EvDo rev. B";
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "CDMA - 1xRTT";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "CDMA - eHRPD";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "iDEN";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPA+";
default:
return "UNKNOWN";
}
}
9、获取网络类型名称:
/** * 解压一个压缩文档 到指定位置 * * @param zipFileString 压缩包的名字 * @param outPathString 指定的路径 * @throws Exception */
public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {
java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
java.util.zip.ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
folder.mkdirs();
} else {
java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
file.createNewFile();
// get the output stream of the file
java.io.FileOutputStream out = new java.io.FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}//end of while
inZip.close();
}//end of func
10、获取系统信息:
String archiveFilePath="sdcard/download/Law.apk";//安装包路径
PackageManager pm = getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
if(info != null){
ApplicationInfo appInfo = info.applicationInfo;
String appName = pm.getApplicationLabel(appInfo).toString();
String packageName = appInfo.packageName; //得到安装包名称
String version=info.versionName; //得到版本信息
Toast.makeText(test4.this, "packageName:"+packageName+";version:"+version, Toast.LENGTH_LONG).show();
Drawable icon = pm.getApplicationIcon(appInfo);//得到图标信息
TextView tv = (TextView)findViewById(R.id.tv); //显示图标
tv.setBackgroundDrawable(icon);