Android_CodeWiki_03
1、发送不重复的通知(Notification)
public static void sendNotification(Context context, String title,
String message, Bundle extras) {
Intent mIntent = new Intent(context, FragmentTabsActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.putExtras(extras); int requestCode = (int) System.currentTimeMillis(); PendingIntent mContentIntent = PendingIntent.getActivity(context,
requestCode, mIntent, 0); Notification mNotification = new NotificationCompat.Builder(context)
.setContentTitle(title).setSmallIcon(R.drawable.app_icon)
.setContentIntent(mContentIntent).setContentText(message)
.build();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults = Notification.DEFAULT_ALL; NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(requestCode, mNotification);
}
代码说明:
关键点在这个requestCode,这里使用的是当前系统时间,巧妙的保证了每次都是一个新的Notification产生。
2、代码设置TextView的样式
使用过自定义Dialog可能马上会想到用如下代码:
new TextView(this,null,R.style.text_style);
但你运行这代码你会发现毫无作用!正确用法:
new TextView(new ContextThemeWrapper(this, R.style.text_style))
3、 ip地址转成8位十六进制串
/** ip转16进制 */
public static String ipToHex(String ips) {
StringBuffer result = new StringBuffer();
if (ips != null) {
StringTokenizer st = new StringTokenizer(ips, ".");
while (st.hasMoreTokens()) {
String token = Integer.toHexString(Integer.parseInt(st.nextToken()));
if (token.length() == )
token = "" + token;
result.append(token);
}
}
return result.toString();
} /** 16进制转ip */
public static String texToIp(String ips) {
try {
StringBuffer result = new StringBuffer();
if (ips != null && ips.length() == ) {
for (int i = ; i < ; i += ) {
if (i != )
result.append('.');
result.append(Integer.parseInt(ips.substring(i, i + ), ));
}
}
return result.toString();
} catch (NumberFormatException ex) {
Logger.e(ex);
}
return "";
} ip:192.168.68.128 16 =>hex :c0a84480
4、WebView保留缩放功能但隐藏缩放控件
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
if (DeviceUtils.hasHoneycomb())
mWebView.getSettings().setDisplayZoomControls(false); 注意:setDisplayZoomControls是在API Level 11中新增。
5、获取网络类型名称
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";
}
}
6、Android解压Zip包
/**
* 解压一个压缩文档 到指定位置
*
* @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
7、 从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;
}
Android_CodeWiki_03的更多相关文章
随机推荐
- ios 文字图标
如何使用自定义字体 在讲icon font之前,首先先来看看普通自定义字体是如何在ios中使用的,两个原理是一样的.这里以KaushanScript-Regular为例: Step 1: 导入字体文件 ...
- [Linked List]Partition List
Total Accepted: 53879 Total Submissions: 190701 Difficulty: Medium Given a linked list and a value x ...
- Log4j.properties配置详细解读
Log4j.properties配置 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以 ...
- Android studio GPU Monitor :GPU Profiling needs to be enabled in the device's developer options
Android studio GPU Monitor 在真机上不能使用,提示:GPU Profiling needs to be enabled in the device's developer o ...
- WCF宿主实践入门
本篇属于WCF实践入门,由于博主本人水平有限,没有理论上的介绍,仅仅从其几种不同的宿主方式分别介绍WCF的使用. WCF有多种宿主方式:1.自托管宿主,2.windows service宿主,3.II ...
- 循环-10. 求序列前N项和*
/* * Main.c * C10-循环-10. 求序列前N项和 * Created on: 2014年7月30日 * Author: Boomkeeper *******部分通过******* */ ...
- asp.net 获取当月的第一天和最后一天示例
DateTime now = DateTime.Now; DateTime d1 = ); DateTime d2 = d1.AddMonths().AddDays(-); d1是本月的第一天,d2本 ...
- java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...
- struts2_20140720
有这样的感觉:前面学的东西弄会了,过了一段时间又感觉陌生了,还要重新开始.这次想个好办法,把写的程序用博客记录下来,把自己的学历历程用博客的形式呈现出来,一来可以方便复习,而来可以以后开发程序可以快速 ...
- Opencv2.4.4作图像旋转和缩放
关于下面两个主要函数的讲解: cv::getRotationMatrix2D(center, angle, scale); cv::warpAffine(image, rotateImg, rotat ...