热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android实用功能介绍,Android几个实用的小功能实现

获取当前活动的activity所在的包名publicStringgetAppPackageName(Contextcontext){ActivityManageractivityM

//获取当前活动的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

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

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

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

if (sTemp.length() <2)

sb.append(0);

sb.append(sTemp.toUpperCase());

}

return sb.toString();

}

//读取系统属性

String getproduct &#61; getSystemProperty("ro.epay.serial","1");

/**

* 读取属性

* */

public String getSystemProperty(String key, String def) {

String result &#61; null;

try {

Class spCls &#61; Class.forName("android.os.SystemProperties");

Class[] typeArgs &#61; new Class[2];

typeArgs[0] &#61; String.class;

typeArgs[1] &#61; String.class;

Constructor spcs &#61; spCls.getConstructor(null);

Object[] valueArgs &#61; new Object[2];

valueArgs[0] &#61; key;

valueArgs[1] &#61; def;

Object sp &#61; spcs.newInstance(null);

Method method &#61; spCls.getMethod("get", typeArgs);

result &#61; (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;

}



推荐阅读
author-avatar
浪人-zhao_433
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有