聲明
歡迎轉載,但請保留文章原始出處:)
部落格園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
正文
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() == 1)
token = "0" + 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() == 8) {
for (int i = 0; i < 8; i += 2) {
if (i != 0)
result.append('.');
result.append(integer.parseint(ips.substring(i, i + 2), 16));
}
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 = "";
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) {
inputstream is = ctx.getresources().getassets().open(filename);
return drawable.createfromstream(is, null);
} catch (ioexception e) {
if (e != null) {
e.printstacktrace();
} catch (outofmemoryerror e) {
return null;
轉自:http://www.cnblogs.com/over140/p/3133262.html