markdown如果出现<>,会将其转为dom标签,导致其中的内容无法正常显示
正文中使用尖括号,可以使用转义字符 < < > >
如 需要添加一个<div>
写成 需要添加一个<div>
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,用起来很简单
要使用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会自动重启)
1.在语言列表中增加中文选项
1 |
$ vim /var/ipfire/langs/list |
在最后面增加zh:简体中文:中国
,最后的内容如下所示:
1 2 3 4 5 6 7 8 9 10 |
en:English:English de:Deutsch:German fr:Français:French es:Español:Spanish pl:Polski:Polish ru:Русский:Russian nl:Nederlands:Dutch tr:Türkçe:Turkish it:Italiano:Italian zh:简体中文:中国 |
2.复制英文语言文件为中文文件
1 |
$ cp /var/ipfire/langs/en.pl /var/ipfire/langs/zh-cn.pl |
3.修改需要显示的英文为中文,注意,里面的内容,前面为键值,后面为显示的内容,比如:
1 |
'Class' => 'Class' |
修改为:
1 |
'Class' => '分类' |
4.修改完成后,刷新语言文件缓存
1 |
$ perl -e "require '/var/ipfire/lang.pl'; &Lang::BuildCacheLang" |
5.在网页中的GUI Setings
中选择语言为简体中文即可。