专栏名称: 程序员大咖
为程序员提供最优质的博文、最精彩的讨论、最实用的开发资源;提供最新最全的编程学习资料:PHP、Objective-C、Java、Swift、C/C++函数库、.NET Framework类库、J2SE API等等。并不定期奉送各种福利。
目录
相关文章推荐
51好读  ›  专栏  ›  程序员大咖

Android开发中常用到的工具类整理

程序员大咖  · 公众号  · 程序员  · 2018-05-20 10:24

正文

请到「今天看啥」查看全文


isOpen;        LocationManager locationManager = (LocationManager) context                .getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)||locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){            isOpen= true ;        } else {            isOpen = false ;        } return isOpen;    }

/**     * 跳转GPS设置     */ public static void openGPSSettings ( final Context context) {
if (checkGPSIsOpen(context)) {
//            initLocation(); //自己写的定位方法 } else { //            //没有打开则弹出对话框 AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogCustom);            builder.setTitle( "温馨提示" );            builder.setMessage( "当前应用需要打开定位功能。请点击"设置"-"定位服务"打开定位功能。" );
//设置对话框是可取消的 builder.setCancelable( false );            builder.setPositiveButton( "设置" , new DialogInterface.OnClickListener() {
@Override public void onClick (DialogInterface dialogInterface, int i) {                    dialogInterface.dismiss(); //跳转GPS设置界面 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                    context.startActivity(intent);                }            });            builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() {
@Override public void onClick (DialogInterface dialogInterface, int i) {                    dialogInterface.dismiss();                    ActivityManager.getInstance().exit();                }            });            AlertDialog alertDialog = builder.create();            alertDialog.show();        }    }

/**     * 字符串进行Base64编码     * @param str     */ public static String StringToBase64 (String str) {        String encodedString = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
return encodedString;    }

/**     * 字符串进行Base64解码     * @param encodedString     * @return */ public static String Base64ToString (String encodedString) {        String decodedString = new String(Base64.decode(encodedString,Base64.DEFAULT));
return decodedString;    }

这里还有一个根据经纬度计算两点间真实距离的,一般都直接使用所集成第三方地图SDK中包含的方法,这里还是给出代码

/**     * 补充:计算两点之间真实距离     *     * @return 米     */    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
       // 维度        double lat1 = (Math.PI / 180) * latitude1;        
       double lat2 = (Math.PI / 180) * latitude2;    
          
       // 经度        double lon1 = (Math.PI / 180) * longitude1;        
       double lon2 = (Math.PI / 180) * longitude2;  
            
       // 地球半径        double R = 6371;        
       
       // 两点间距离 km,如果想要米的话,结果*1000就可以了        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;        
       return d * 1000;
           }


常用文件类

文件类的代码较多,这里就只给出读写文件的

/**     * 判断SD卡是否可用     * @return SD卡可用返回true     */    public static boolean hasSdcard() {        String status = Environment.getExternalStorageState();
       return Environment.MEDIA_MOUNTED.equals(status);    }  
        
   /**     * 读取文件的内容     *
    * 默认utf-8编码     * @param filePath 文件路径     * @return 字符串     * @throws IOException     */
   public static String readFile(String filePath) throws IOException {        
       return readFile(filePath, "utf-8");    }  
        
   /**     * 读取文件的内容     * @param filePath 文件目录     * @param charsetName 字符编码     * @return String字符串     */    public static String readFile(String filePath, String charsetName)            throws IOException {        
       if (TextUtils.isEmpty(filePath))            
           return null;        
       if (TextUtils.isEmpty(charsetName))            charsetName = "utf-8";            File file = new File(filePath);            StringBuilder fileContent = new StringBuilder("");        
       if (file == null || !file.isFile())            
           return null;            BufferedReader reader = null






请到「今天看啥」查看全文