声明

欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com

农民伯伯: http://over140.cnblogs.com

正文

1、发送不重复的通知(Notification)

public static voidsendNotification(Contextcontext,Stringtitle,
Stringmessage,Bundleextras){
IntentmIntent= newIntent(context,FragmentTabsActivity. class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.putExtras(extras);

intrequestCode=( int)System.currentTimeMillis();

PendingIntentmContentIntent=PendingIntent.getActivity(context,
requestCode,mIntent,0);

NotificationmNotification= newNotificationCompat.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;

NotificationManagermNotificationManager=(NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(requestCode,mNotification);
}

代码说明:

关键点在这个requestCode,这里使用的是当前系统时间,巧妙的保证了每次都是一个新的Notification产生。

2、代码设置TextView的样式

使用过自定义Dialog可能马上会想到用如下代码:

newTextView(this,null,R.style.text_style);

但你运行这代码你会发现毫无作用!正确用法:

newTextView( newContextThemeWrapper( this,R.style.text_style))

来自这里。

3、 ip地址转成8位十六进制串

/** ip转16进制 */
public staticStringipToHex(Stringips){
StringBufferresult= newStringBuffer();
if(ips!= null){
StringTokenizerst= newStringTokenizer(ips,".");
while(st.hasMoreTokens()){
Stringtoken=Integer.toHexString(Integer.parseInt(st.nextToken()));
if(token.length()==1)
token="0"+token;
result.append(token);
}
}
returnresult.toString();
}

/** 16进制转ip */
public staticStringtexToIp(Stringips){
try{
StringBufferresult= newStringBuffer();
if(ips!= null&&ips.length()==8){
for( inti=0;i<8;i+=2){
if(i!=0)
result.append('.');
result.append(Integer.parseInt(ips.substring(i,i+2),16));
}
}
returnresult.toString();
} catch(NumberFormatExceptionex){
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 staticStringgetNetworkTypeName(Contextcontext){
if(context!= null){
ConnectivityManagerconnectMgr=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectMgr!= null){
NetworkInfoinfo=connectMgr.getActiveNetworkInfo();
if(info!= null){
switch(info.getType()){
caseConnectivityManager.TYPE_WIFI:
return"WIFI";
caseConnectivityManager.TYPE_MOBILE:
returngetNetworkTypeName(info.getSubtype());
}
}
}
}
returngetNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);
}

public staticStringgetNetworkTypeName( inttype){
switch(type){
caseTelephonyManager.NETWORK_TYPE_GPRS:
return"GPRS";
caseTelephonyManager.NETWORK_TYPE_EDGE:
return"EDGE";
caseTelephonyManager.NETWORK_TYPE_UMTS:
return"UMTS";
caseTelephonyManager.NETWORK_TYPE_HSDPA:
return"HSDPA";
caseTelephonyManager.NETWORK_TYPE_HSUPA:
return"HSUPA";
caseTelephonyManager.NETWORK_TYPE_HSPA:
return"HSPA";
caseTelephonyManager.NETWORK_TYPE_CDMA:
return"CDMA";
caseTelephonyManager.NETWORK_TYPE_EVDO_0:
return"CDMA-EvDorev.0";
caseTelephonyManager.NETWORK_TYPE_EVDO_A:
return"CDMA-EvDorev.A";
caseTelephonyManager.NETWORK_TYPE_EVDO_B:
return"CDMA-EvDorev.B";
caseTelephonyManager.NETWORK_TYPE_1xRTT:
return"CDMA-1xRTT";
caseTelephonyManager.NETWORK_TYPE_LTE:
return"LTE";
caseTelephonyManager.NETWORK_TYPE_EHRPD:
return"CDMA-eHRPD";
caseTelephonyManager.NETWORK_TYPE_IDEN:
return"iDEN";
caseTelephonyManager.NETWORK_TYPE_HSPAP:
return"HSPA+";
default:
return"UNKNOWN";
}
}

6、Android解压Zip包

/**
*解压一个压缩文档到指定位置
*
*
@param zipFileString压缩包的名字
*
@param outPathString指定的路径
*
@throws Exception
*/
public static voidUnZipFolder(StringzipFileString,StringoutPathString) throwsException{
java.util.zip.ZipInputStreaminZip= newjava.util.zip.ZipInputStream( newjava.io.FileInputStream(zipFileString));
java.util.zip.ZipEntryzipEntry;
StringszName="";

while((zipEntry=inZip.getNextEntry())!= null){
szName=zipEntry.getName();

if(zipEntry.isDirectory()){

// getthefoldernameofthewidget
szName=szName.substring(0,szName.length()-1);
java.io.Filefolder= newjava.io.File(outPathString+java.io.File.separator+szName);
folder.mkdirs();

} else{

java.io.Filefile= newjava.io.File(outPathString+java.io.File.separator+szName);
file.createNewFile();
// gettheoutputstreamofthefile
java.io.FileOutputStreamout= newjava.io.FileOutputStream(file);
intlen;
byte[]buffer= new byte[1024];
// read(len)bytesintobuffer
while((len=inZip.read(buffer))!=-1){
// write(len)bytefrombufferattheposition0
out.write(buffer,0,len);
out.flush();
}
out.close();
}
} // endofwhile

inZip.close();

} // endoffunc

7、从assets中读取文本和图片资源

/** 从assets文件夹中读取文本数据 */
public staticStringgetTextFromAssets( finalContextcontext,StringfileName){
Stringresult="";
try{
InputStreamin=context.getResources().getAssets().open(fileName);
// 获取文件的字节数
intlenght=in.available();
// 创建byte数组
byte[]buffer= new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result=EncodingUtils.getString(buffer,"UTF-8");
in.close();
} catch(Exceptione){
e.printStackTrace();
}
returnresult;
}

/** 从assets文件夹中读取图片 */
public staticDrawableloadImageFromAsserts( finalContextctx,StringfileName){
try{
InputStreamis=ctx.getResources().getAssets().open(fileName);
returnDrawable.createFromStream(is, null);
} catch(IOExceptione){
if(e!= null){
e.printStackTrace();
}
} catch(OutOfMemoryErrore){
if(e!= null){
e.printStackTrace();
}
} catch(Exceptione){
if(e!= null){
e.printStackTrace();
}
}
return null;
}

系列

Android实用代码七段(三)

Android实用代码七段(二)

Android实用代码七段(一)

更多相关文章

  1. Gradle 3.1 修改apk文件名和输出路径
  2. CentOS7上编译Android系统
  3. Android(安卓)近百个项目的源代码,覆盖Android开发的每个领域
  4. 【stagefrightplayer】1 调用过程
  5. ubuntu10.10下的android源码下载及编译
  6. Android(安卓)alertdialog的按钮点击后不消失
  7. Android(安卓)开发笔记1 (MTK)
  8. Android(安卓)AOSP 环境下实现C++直接调用libmedia.so接口播放视
  9. android studio 默认 .gitignore 文件模板

随机推荐

  1. Python:eval()将值强制转换为浮点数?
  2. Windows7下Jupyter Notebook使用入门
  3. 机器学习教程之2-k近邻模型的sklearn实现
  4. 我无法使用GAE在Windows上以本地模式运行
  5. 7-13 日K蜡烛图
  6. Python 文件的基本操作
  7. 使用Python+PIL查看两张相似图形的差异
  8. eclipse中写python文件找不到
  9. openpyxl读取excel中公式的结果值
  10. caffe中各语言预处理对应方式