今天遇到一个错误,莫名戳中笑点,好贴心好长的提示。
原因:使用application startActivity,必须使用FLAG_ACTIVITY_NEW_TASK,因为新起来的Activity没办法和application放到一个task中,必须用FLAG_ACTIVITY_NEW_TASK让新启动的ACTIVITY独占一个TASK。
今天遇到一个错误,莫名戳中笑点,好贴心好长的提示。
原因:使用application startActivity,必须使用FLAG_ACTIVITY_NEW_TASK,因为新起来的Activity没办法和application放到一个task中,必须用FLAG_ACTIVITY_NEW_TASK让新启动的ACTIVITY独占一个TASK。
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。
markdown如果出现<>,会将其转为dom标签,导致其中的内容无法正常显示
正文中使用尖括号,可以使用转义字符 < < > >
如 需要添加一个<div>
写成 需要添加一个<div>
1.dependencies中有provided和compile两种类型
provided就是编译时依赖,但打包apk时不把包打进去。
应用场景,给第三方提供的sdk,编译依赖某些jar包,但实际使用jar包由第三方控制。
compile 就是编译且打包进apk
compile还有两个衍生
-compile files 本地jar包
-compile project 源码依赖
在Android系统中,当SD卡挂载在电脑上时候,如果手动将语音备忘录中的录音删除的时,那么相应数据库中的数据也是需要修改的。此时实现需要对挂载进行监听,需要继承BroadcastReceiver类,实现其中的onRecieve(Context context, Intent inten)方法。代码如下:
|
1 2 3 4 5 6 7 8 9 10 |
public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_EJECT)) { System.out.println("-------------------> mount ACTION_MEDIA_EJECT"); } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { //TODO: System.out.println("-------------------> mount ACTION_MEDIA_MOUNTED"); } } |
此时须在Manifest中进行注册:
|
1 2 3 4 5 6 7 |
<receiver android:name=".ExternalStorageListener"> <intent-filter> <action android:name="android.intent.action.MEDIA_EJECT" /> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <data android:scheme="file"/> </intent-filter> </receiver> |
1.Properties类与Properties配置文件
Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。
2.Properties中的主要方法
(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
|
1 2 3 4 |
Properties pro = new Properties(); FileInputStream in = new FileInputStream("a.properties"); pro.load(in); in.close(); |
(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
|
1 2 3 |
FileOutputStream oFile = new FileOutputStream(file, "a.properties"); pro.store(oFile, "Comment"); oFile.close(); |
如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。
注释信息后面是属性文件的当前保存时间信息。
(3)getProperty/setProperty
这两个方法是分别是获取和设置属性信息。
3.代码实例
属性文件a.properties如下:
|
1 2 3 |
name=root pass=liu key=value |
读取a.properties属性列表,与生成属性文件b.properties。代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Iterator; import java.util.Properties; public class PropertyTest { public static void main(String[] args) { Properties prop = new Properties(); try{ //读取属性文件a.properties InputStream in = new BufferedInputStream (new FileInputStream("a.properties")); prop.load(in); ///加载属性列表 Iterator<String> it=prop.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); System.out.println(key+":"+prop.getProperty(key)); } in.close(); ///保存属性到b.properties文件 FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开 prop.setProperty("phone", "10086"); prop.store(oFile, "The New properties file"); oFile.close(); } catch(Exception e){ System.out.println(e); } } } |
最近在开发Android时遇到插U盘获取U盘内容的需求,但是按照传统的Environment.getExternalStorageDirectory()只能读取到插入的SD卡的路径,如果是U盘的话无法读出U盘的路径。
最终在一个在CSDN的论坛里找到相关的东西,就试了下直接通过StorageManager获取存储路径的。
核心如下,volumePaths的数组就是系统外接设备的路径,经过测试的确是挂载的路径。不过有些是不可用的,它只是列出了系统可支持的外接路径。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
final StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); String[] volumePaths = new String[0]; try { final Method method = sm.getClass().getMethod("getVolumePaths"); if(null != method) { method.setAccessible(true); volumePaths = (String[]) method.invoke(sm); } }catch (Exception e){ e.printStackTrace(); } if ((volumePaths != null) && (volumePaths.length > 0)){ for (String sdcardPath : volumePaths){ Log.d(TAG,"sdcardPath:" + sdcardPath); } } |
工程引入了jar包,jar包使用了R.layout.xx,但这个layout并没有打进jar包导致找不到R.layout.xx。
It's because inside JAR doesn't contain resource folder of SDK Project.
解决方法有两种:
如果要把jar包提供出去,则需要使用方法2
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
获取指定已安装完整签名信息,包括MD5指纹:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public void getSingInfo() { try { PackageInfo packageInfo = getPackageManager().getPackageInfo("com.sina.weibo", PackageManager.GET_SIGNATURES); Signature[] signs = packageInfo.signatures; Signature sign = signs[0]; parseSignature(sign.toByteArray()); } catch (Exception e) { e.printStackTrace(); } } public void parseSignature(byte[] signature) { try { CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature)); String pubKey = cert.getPublicKey().toString(); String signNumber = cert.getSerialNumber().toString(); System.out.println("signName:" + cert.getSigAlgName()); System.out.println("pubKey:" + pubKey); System.out.println("signNumber:" + signNumber); System.out.println("subjectDN:"+cert.getSubjectDN().toString()); } catch (CertificateException e) { e.printStackTrace(); } } |
在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,用起来很简单