statfs获得硬盘信息

statfs结构如下:

struct statfs
{
long    f_type;     /* 文件系统类型  */
long    f_bsize;    /* 经过优化的传输块大小  */
long    f_blocks;   /* 文件系统数据块总数 */
long    f_bfree;    /* 可用块数 */
long    f_bavail;   /* 非超级用户可获取的块数 */
long    f_files;    /* 文件结点总数 */
long    f_ffree;    /* 可用文件结点数 */
fsid_t  f_fsid;     /* 文件系统标识 */
long    f_namelen;  /* 文件名的最大长度 */
};

statfs接口调用成功返回0,失败返回-1.

简单用法:
int resultCode = statfs(path.c_str(), &data);
if (resultCode < 0) {
return false;
} else {
unsigned long freeSize = data.f_bsize * data.f_bfree;
unsigned long availSize = data.f_bsize * data.f_bavail;
unsigned long totalSize = data.f_bsize * data.f_blocks;
}

单位bytes,使用>>20转为MB。

NoClassdefFoundError R$layout

工程引入了jar包,jar包使用了R.layout.xx,但这个layout并没有打进jar包导致找不到R.layout.xx。

It's because inside JAR doesn't contain resource folder of SDK Project.

解决方法有两种:

  1. 导入SDK Project,并以lib的形式依赖。

    如果要把jar包提供出去,则需要使用方法2

  2. 首先,使用下面的方法替代R(在SDK中,R自动为SDK的包名,引入他人工程后肯定找不到这个R,所以不能在SDK中使用R)

    public static int getResourseIdByName(String packageName, String className, String name) {
    Class r = null;
    int id = 0;
    try {
    r = Class.forName(packageName + ".R");

    Class[] classes = r.getClasses();
    Class desireClass = null;

    for (int i = 0; i < classes.length; i++) {
    if(classes[i].getName().split("\\$")[1].equals(className)) {
    desireClass = classes[i];

    break;
    }
    }

    if(desireClass != null)
    id = desireClass.getField(name).getInt(desireClass);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (NoSuchFieldException e) {
    e.printStackTrace();
    }

    return id;

    }

    举例,如果以前使用了 R.layout.main,现在需要使用getResourseIdByName(context.getPackageName(), "layout", "main")      以前使用了R.id.mView,现在需要使用getResourseIdByName(context.getPackageName(), "id", "mView")

然后,把用到的资源从SDK中copy到Apk工程。

源自http://stackoverflow.com/questions/14373004/java-lang-noclassdeffounderror-com-facebook-android-rlayout-error-when-using-f

 

Android获得内存信息

在Android中,想要获得进程内存信息,有两类方法

1.exec大法,使用Runtime.getRuntime().exec()方法来执行命令行,主要命令行有 dumpsys(需要system权限)  cat /proc等

private String catProc() {
StringBuilder meminfo = new StringBuilder();
try {
ArrayList<String> commandLine = new ArrayList<String>();
commandLine.add("cat");
// commandLine.add("/proc/meminfo");
commandLine.add("/proc/" + android.os.Process.myPid() + "/status");

Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = bufferedReader.readLine()) != null) {
meminfo.append(line);
meminfo.append("\n");
}

} catch (IOException e) {
Log.e(TAG, "Could not read /proc/meminfo", e);
}

Log.i(TAG, "showMeminfo = " + meminfo.toString());

return meminfo.toString();
}

2.android.os.Debug

Debug类有大量的获取内存信息方法,如getPss,用起来很简单

 

使用jar包中的service

要使用jar包或lib中的service,

假如 app包名为 com.app.xx

jar包名为 com.jar.xx

需要在当前app的manifest中声明service,且使用jar包中的包名,全路径,

android:name="com.jar.xx.xxService"

同时声明exported为true

android:exported="true"

<service
android:name="com.jar.xx.xxService"
android:enabled="true"
android:exported="true" />

之后就可以使用am指令打开/关闭service了

打开service ()
adb shell am startservice -n com.app.xx/com.jar.xx.xxService
关闭service
1.adb shell am stopservice -n com.app.xx/com.jar.xx.xxService
2.可能android低版本会不支持 stopservice命令。备用关闭方法: adb shell am force-stop com.app.xx (会关闭整个进程,用kill进程不行,service会自动重启)

Android:如何重启app?

重启App,目前有两种办法:

1.使用 PendingIntent,设置在未来某个时刻启动你的app,然后关掉app。

Intent mStartActivity = new Intent(context, MainActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)MeConfigs.context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);

2.使用NEW_TASK的flag打开ActivityB,然后关掉当前进程,ActivityB马上再打开app。

重启代码

Intent i = new Intent(getApplicationContext(), DummyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Process.killProcess(Process.myPid());

ActivityB代码

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   
   Intent i = new Intent();
   i.setData(Uri.parse("xxx"));
   i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(i);
   finish();
}

JSON相关:JSON-bundle

JSON转成bundle

Intent intent = new Intent();
JSONObject extrasJson = new JSONObject(extras);
Iterator<String> keys=extrasJson.keys();
while (keys.hasNext()) {
    String key = keys.next();
    // 可以根据opt出来的不同类型put
    intent.put(key, extrasJson.optString(key));
}

这里有一个需要注意的地方,SDK19以后,用wrap可以更好的处理get出来是map、array等问题。

bundle转JSON 也就是遍历bundle

JSONObject json = new JSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
    try {
        // json.put(key, bundle.get(key)); see edit below
        json.put(key, JSONObject.wrap(bundle.get(key)));
    } catch(JSONException e) {
        //Handle exception here
    }
}