1.点击back键,连续两次时间间隔小于2s才能退出app
成员变量 exitTime初始值为0
longcurrentTime = System.currentTimeMillis();
if((currentTime -exitTime) <2000) {
super.onBackPressed();
}else {
Toast.makeText(this, R.string.double_click_exit, Toast.LENGTH_SHORT).show();
exitTime= currentTime;
}
2.5.0之后出来的抽屉布局,我们想要实现,左面出来的布局,有一个头布局和菜单按钮;当然我们可以通过自己写布局来实现,但是谷歌官方提供了我们一个现成的控件:NavigationView,极大的方便了我们开发.


看两个属性app:headerLayout="@layout/nav_header_main"
app:menu="@menu/nav_menu"
第一个是设置头布局,不多说了;
第二个就是菜单文件;
PS:只是这样做会出现图片全是灰色;让图片就是显示他本身的颜色;
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null);
还有下面两个常用的API:
1.app:itemBackground="@color/colorAccent"设置每一个item的背景颜色
2.app:itemTextColor=""设置item的背景颜色
另外如果想在NavigationView的item之间添加上一条分隔线呢?很简单,只需要在menu中将相应的item放到一个group中,并给该group取一个id即可,代码如下:(本例是这几个按钮在一个group),可以多放几个按钮设置

点击事件
.头部点击事件//获取头布局文件
View headerView = navigationView.getHeaderView(0);
2.item点击事件navigationView.setNavigationItemSelectedListener(newNavigationView.OnNavigationItemSelectedListener() {
@Override
publicbooleanonNavigationItemSelected(MenuItem item) {
//在这里处理item的点击事件
returntrue;
}
});

不定期添加小功能
获取存储地址,第二个参数可以认为是文件夹名称
public File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
当SD卡存在或者SD卡不可被移除的时候,就调用getExternalCacheDir()方法来获取缓存路径,否则就调用getCacheDir()方法来获取缓存路径。前者获取到的就是 /sdcard/Android/data/package>/cache 这个路径,而后者获取到的是 /data/data/package>/cache 这个路径。
获取版本号码
public int getAppVersion(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
MD5加密
public String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
每个应用程序最高可用内存是多少。
intmaxMemory = (int) (Runtime.getRuntime().maxMemory() /1024);
跳转到应用详情界面;
Intent intent =newIntent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(),null);
intent.setData(uri);
startActivityForResult(intent,101);

2018年1月23日
升级到了3.0debug打包无法直接运行在手机上,后来使用Build-Build APK(s)发现可以了,记录下。

网友评论