1 UUID
UUID uuid = UUID.randomUUID();
2 锁屏后系统杀死app
锁屏一段时间后,系统会把高耗电的app杀死,网上收了很多代码的解决方法(变成前台进程或者唤醒cpu),发现不起作用,在360F4移动版中30分钟后仍会把app杀死,解决发发如下:
进入设置–>省电管理/电池管理–>锁屏保护程序–>添加自己的app。
发现50分钟后,仍然不会被系统kill。
3 dp与px的转换
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
4 Style
<Button
...
style="?android:attr/buttonBarButtonStyle" />
5 占位符
setTitle(getString(R.string.index, index + 1, pageCount));
<resources>
<string name="index">PdfRendererBasic (%1$d/%2$d)</string>
</resources>
state = String.format(touchType + ",当前经度: %f 当前纬度:%f", currentPt.longitude, currentPt.latitude);
6 assets目录
7 手机桌面有多个快捷方式
如果在Activity_A、Activity_B都注册了启动的intent-filter
,那么桌面会出现2个快捷方式。
<activity android:name=".activity.WelcomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".activity.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
8 String.valueOf(boolean b))
Log.d(TAG,"true="+String.valueOf(true));
Log.d(TAG,"false="+String.valueOf(false));
值:
9 btn.setError(“错误”);
btn.setError("错误");
10 类BaseAdapter
中可以在getView中调用notifyDataSetChange()
seletor中的颜色切换
如果是Text的颜色切换,那么用color;如果是控件背景色的切换,必须用drawable
text_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#FF6F00" />
<item android:color="#0D47A1"/>
</selector>
text_bottom_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color2" android:state_checked="true"/>
<item android:drawable="@color/color1"/>
</selector>
11 按home键后点击app快捷方式,进入的界面不是退出时的界面
问题:打开app–>新进入A界面–>再进入B界面–>再按home键–>点击app的快捷方式,先出现的是A界面,然后才是B界面。
目标:现在我们要是“按完home键,点击app快捷方式”,出现的是消失前的B界面,而不是A界面
方法:
方法一:给intent添加Flag
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
方法二:添加以下代码:放在那里都可以
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
参考:
FLAG_ACTIVITY_BROUGHT_TO_FRONT与FLAG_ACTIVITY_REORDER_TO_FRONT区别
Android按home键之后,再次点击程序图标避免再次重启程序
12 post( )
btn.post(new Runnable() {
@Override
public void run() {
//UI Thread
}
});
13 Android域名转IP
try {
NetUrl.BASE_URL = InetAddress.getByName("").getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}finally {
NetUrl.BASE_URL = "http://139.219.188.45/itip/";
}
14 使用域名获取服务器数据
做App一直以来都是考ip地址来获取服务器数据,以为域名只是在web端可以,想不到在Android端也可以,而且不需要转化成ip,直接使用即可
15 Android常见路径
方法 | 说明 |
---|---|
Environment.getExternalStorageDirectory().getAbsolutePath() | /storage/emulated/0 |
getPackageCodePath() | /data/app/com.cqc.tablayout01-2/base.apk |
getPackageResourcePath() | /data/app/com.cqc.tablayout01-2/base.apk |
getCacheDir().getAbsolutePath() | /data/data/com.cqc.tablayout01/cache |
getCodeCacheDir().getAbsolutePath(),API>=21 | /data/data/com.cqc.tablayout01/code_cache |
getFilesDir().getAbsolutePath() | /data/data/com.cqc.tablayout01/files |
getExternalCacheDir().getAbsolutePath() | /storage/emulated/0/Android/data/com.cqc.tablayout01/cache |
getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() | /storage/emulated/0/Android/data/com.cqc.tablayout01/files/Pictures |
16 怎样查找官方各种包的版本号
比如要使用recyclerview,我想用24.0.0这个版本,那这个版本号对吗?怎么查找他们的版本号?
取sdk中查找,\SDK\extras\android\m2repository\com\android\support\recyclerview-v7,里面会有版本号
17 url拼接
http://106.14.xx.xx:8xx5/xxx.a?driverTel=15000000000