Android实用代码七段(四)

简介:

声明

欢迎转载,但请保留文章原始出处:) 
博客园: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 = "";
         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;
    }
复制代码

 

转自:http://www.cnblogs.com/over140/p/3133262.html

目录
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
3月前
|
人工智能 IDE 开发工具
Studio Bot - 让 AI 帮我写 Android 代码
Studio Bot - 让 AI 帮我写 Android 代码
152 1
|
9月前
|
IDE 数据可视化 Java
Android自动生成代码,可视化脚手架之基础信息配置
今天的内容比较简单,大致过一下Electron一些基本用法,虽然说这些比较简单,但又是不得不去了解的,正如做Android的我们,也不是一上来就会的,需要一个循序渐进的过程,下一章,我们再去实际的开发功能。
129 0
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
47 0
|
3月前
|
安全 算法 JavaScript
安卓逆向 -- 关键代码定位与分析技术
安卓逆向 -- 关键代码定位与分析技术
38 0
|
3月前
|
安全 Android开发 数据安全/隐私保护
代码安全之代码混淆及加固(Android)
代码安全之代码混淆及加固(Android)
40 0
|
4月前
|
安全 Java Android开发
Android App开发之安全加固中反编译、代码混淆、第三方加固以及重签名的讲解及实战(图文解释 简单易懂)
Android App开发之安全加固中反编译、代码混淆、第三方加固以及重签名的讲解及实战(图文解释 简单易懂)
70 0
|
6月前
|
XML Java Android开发
Android 解决使用CocosCreator开发产品上架应用市场代码重复问题
Android 解决使用CocosCreator开发产品上架应用市场代码重复问题
271 0
|
7月前
|
JSON dexposed Java
一文总结 Android 隐私合规代码思路
一文总结 Android 隐私合规代码思路
|
7月前
|
SQL 程序员 Android开发
一行代码,利用 android studio自带的 liveTemplate 快速生成单例模式,程序员偷懒神器
一行代码,利用 android studio自带的 liveTemplate 快速生成单例模式,程序员偷懒神器