美文网首页
Android几个实用的小功能实现

Android几个实用的小功能实现

作者: 未丑 | 来源:发表于2018-06-06 11:05 被阅读0次

//获取当前活动的activity所在的包名

public String getAppPackageName(Context context){

 ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 

 List taskInfo = activityManager.getRunningTasks(1);

    ComponentName componentInfo = taskInfo.get(0).topActivity;

    Log.d(TAG, "package:" + componentInfo.getPackageName());

    return componentInfo.getPackageName();

    }   

//获取当前活动activity名称

  public String getRunningActivityName(Context context) {

    ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    //

    String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName();

    String contextActivity = runningActivity.substring(runningActivity.lastIndexOf(".")+1);

    Log.d(TAG, "contextActivity:" + contextActivity);

    return contextActivity;

    } 

//SharedPreferences存取简单数据的例子

public void setStartBTprtValue(String bStart) {

    SharedPreferences mySharedPreferences= getSharedPreferences("StartBTprt",

    Activity.MODE_PRIVATE);

    SharedPreferences.Editor editor = mySharedPreferences.edit();

    editor.putString("bStart", bStart);

    editor.commit();

    }

    public String getStartBTprtValue() {

    SharedPreferences sharedPreferences= getSharedPreferences("StartBTprt",

    Activity.MODE_PRIVATE);

    String bStart =sharedPreferences.getString("bStart", "");

    return bStart;

    }

//数据转换

public int byte2unsignedint(byte a)

    {

        int b = a & 0xff;

        return b;

    }

    public String bytetoString(byte[] bytearray) {

        String result = "";

        char temp;

        int length = bytearray.length;

        for (int i = 0; i < length; i++) {

            temp = (char) bytearray[i];

            result += temp;

        }

        return result;

    }

//java 合并两个byte数组

    public byte[] byteMerger(byte[] byte_1, byte[] byte_2){ 

        byte[] byte_3 = new byte[byte_1.length+byte_2.length]; 

        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); 

        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length); 

        return byte_3; 

    }

//删除文件

public void DeleteFile(File file) {

        if (file.exists() == false) {

         Log.e("www","DeleteFile not exists!!!");

            return;

        } else {

            if (file.isFile()) {

                file.delete();

                Log.e("www","DeleteFile 1 ok!!!");

                return;

            }

            if (file.isDirectory()) {

                File[] childFile = file.listFiles();

                if (childFile == null || childFile.length == 0) {

                    file.delete();

                    return;

                }

                for (File f : childFile) {

                    DeleteFile(f);

                }

                file.delete();

                Log.e("www","DeleteFile ok!!!");

            }

        }

    }

    /** 

    * 复制单个文件 

    * @param oldPath String 原文件路径 如:c:/fqf.txt 

    * @param newPath String 复制后路径 如:f:/fqf.txt 

    * @return 

    */ 

  public void copyFile(String oldPath, String newPath) { 

      try { 

          int bytesum = 0; 

          int byteread = 0; 

          File oldfile = new File(oldPath); 

          if (oldfile.exists()) { //文件不存在时 

              InputStream inStream = new FileInputStream(oldPath);

              FileOutputStream fs = new FileOutputStream(newPath); 

              byte[] buffer = new byte[1444]; 

              while ( (byteread = inStream.read(buffer)) != -1) { 

                  bytesum += byteread; //字节数 文件大小 

                  System.out.println(bytesum); 

                  fs.write(buffer, 0, byteread); 

              } 

              inStream.close();

              fs.close();

          } 

      } 

      catch (Exception e) { 

          System.out.println("复制单个文件操作出错"); 

          e.printStackTrace(); 

       } 

   } 

//write data to file in SD

    public void writeFileSdcardFile(String fileName,String write_str) throws IOException{ 

    try{ 

           FileOutputStream fout = new FileOutputStream(fileName); 

          byte [] bytes = write_str.getBytes(); 

           fout.write(bytes); 

          fout.close(); 

        } 

          catch(Exception e){ 

            e.printStackTrace(); 

          } 

  } 

    //read data from SD

    public String readFileSdcardFile(String fileName) throws IOException{ 

      String res=""; 

      try{ 

            FileInputStream fin = new FileInputStream(fileName); 

             int length = fin.available(); 

             byte [] buffer = new byte[length]; 

            fin.read(buffer);     

             res = EncodingUtils.getString(buffer, "UTF-8"); 

             fin.close();     

            } 

            catch(Exception e){ 

            e.printStackTrace(); 

            } 

            return res; 

    }

    //2 read data from SD//161020

    public byte [] readByteSdcardFile(String fileName) throws IOException{ 

      //String res=""; 

      try{ 

            FileInputStream fin = new FileInputStream(fileName); 

             int length = fin.available(); 

             byte [] buffer = new byte[length]; 

            fin.read(buffer);     

             //res = EncodingUtils.getString(buffer, "UTF-8"); 

             fin.close();

            return buffer;

            } 

            catch(Exception e){ 

            e.printStackTrace(); 

            }

    return null; 

    } 

//run linux cmd

    String do_exec(String cmd) { 

        String s = "/n"; 

        try { 

            Process p = Runtime.getRuntime().exec(cmd); 

            BufferedReader in = new BufferedReader( 

                                new InputStreamReader(p.getInputStream())); 

            String line = null; 

            while ((line = in.readLine()) != null) { 

                s += line + "/n";               

            } 

        } catch (IOException e) { 

            // TODO Auto-generated catch block 

            e.printStackTrace(); 

        } 

        //text.setText(s); 

        return cmd;     

    }   

    public static String str2HexStr(String str) {   

        char[] chars = "0123456789ABCDEF".toCharArray();   

        StringBuilder sb = new StringBuilder(""); 

        byte[] bs = str.getBytes();   

        int bit;   

        for (int i = 0; i < bs.length; i++) {   

            bit = (bs[i] & 0x0f0) >> 4;   

            sb.append(chars[bit]);   

            bit = bs[i] & 0x0f;   

            sb.append(chars[bit]);   

        }   

        return sb.toString();   

    }

    //byteTo16HexStr//161020

    public static String bytesToHexString(byte[] bArray) {

      StringBuffer sb = new StringBuffer(bArray.length);

      String sTemp;

      for (int i = 0; i < bArray.length; i++) {

      sTemp = Integer.toHexString(0xFF & bArray[i]);

      if (sTemp.length() < 2)

        sb.append(0);

      sb.append(sTemp.toUpperCase());

      }

      return sb.toString();

    }

//读取系统属性

String getproduct = getSystemProperty("ro.epay.serial","1");

/**

* 读取属性

* */

public String getSystemProperty(String key, String def) {

String result = null;

try {

Class spCls = Class.forName("android.os.SystemProperties");

Class[] typeArgs = new Class[2];

typeArgs[0] = String.class;

typeArgs[1] = String.class;

Constructor spcs = spCls.getConstructor(null);

Object[] valueArgs = new Object[2];

valueArgs[0] = key;

valueArgs[1] = def;

Object sp = spcs.newInstance(null);

Method method = spCls.getMethod("get", typeArgs);

result = (String) method.invoke(sp, valueArgs);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

}

return result;

}

相关文章

网友评论

      本文标题:Android几个实用的小功能实现

      本文链接:https://www.haomeiwen.com/subject/rexbsftx.html