Android Activity 开发常用技巧整理
1.设置 Activity 背景色为透明
在style.xml里面声明:
<style name="TranslucentActivityStyle" parent="@android:style/Theme.Translucent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
在Activity的onCreate方法中添加:
setTheme(R.style.Theme_Transparent);
注意,此代码需要放在setContentView方法之前。后续是需要在AndroidMainfest的相应Activity里面声明:
android:theme="@style/Theme.Transparent"
2. 如何安全退出已调用多个 Activity 的 Application?
①. 记录打开的Activity,每打开一个Activity,就记录下来,在需要退出的时候,关闭每一个activity。
②. 发送特定的广播,在需要结束应用时,发送一个特定的广播,每一个Activity收到广播后关闭。
③. 通过 Intent 的 flag 来实现。实现intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 调用一个activity。此时如果该任务栈中已经有该 Activity,那么系统会把这个 Activity 上面的所有 Activity 干掉。其实相当于给 Activity 配置的启动模式为 SingleTask。
3.显示和隐藏输入法
private void showKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUrlText, InputMethodManager.SHOW_IMPLICIT);
}
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mUrlText.getWindowToken(), 0);
}
4. Android 6.0以上动态检测请求权限
//检查权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}
5. Android 获取相册图片
Intent innerIntent = new Intent();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
innerIntent.setAction(Intent.ACTION_GET_CONTENT);
} else {
innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
innerIntent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(innerIntent, "选择图片");
this.startActivityForResult(wrapperIntent, REQUEST_CODE);
public String uri2FilePath(Uri uri) {
String path = "";
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(this, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
path = cursor.getString(columnIndex);
}
cursor.close();
}
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
path = cursor.getString(column_index);
}
cursor.close();
}
}
return path;
}
6. Activity调用系统相机录像
使用 Intent 调用系统相机进行视频录制:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// 录制质量 1:高质量 0:低质量
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// 录制时长(单位:秒)
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5 * 60);
startActivityForResult(intent, SYSTEM_CAMREA_RECORD_VIDEO);
录制完成后,可以在 onActivityResult 里面收到录制视频的地址内容:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SYSTEM_CAMREA_RECORD_VIDEO) {
if (resultCode == RESULT_OK) {
Log.e(GlobalConfig.Log_TAG, "录制视频地址 = " + FileUtils.getPath(this, data.getData()));
mRichEditorLayout.addVideoInfo(FileUtils.getPath(this, data.getData()));
}
}
}
7. 判断当前线程是否是主线程
有如下三个方式:
public boolean isMainThread() {
return Looper.getMainLooper() == Looper.myLooper();
}
public boolean isMainThread() {
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
public boolean isMainThread() {
return Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId();
}
8. 进入&退出全屏
进入&退出全屏,主要是状态栏的隐藏和展示。下面是相关的代码实现:
进入全屏:
// 隐藏状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
退出全屏:
// 显示状态栏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Android Activity 开发常用技巧整理的更多相关文章
- HTML5-移动开发常用技巧与弹性布局的使用
一.移动开发常用技巧 Viewport基本知识 设置布局Viewport的各种信息 1.width=device-width: 设置Viewport视口宽度等于设备宽度 2.initial-scale ...
- sqlmap常用技巧整理
言 通过在乌云网上出现的很多SQL注入漏洞,因此来总结一下,大致使用SQLMAP所遇到的参数. 基本结构 基本SQLMAP的使用方式就如下所示,使用参数式的方式,按需求添加. 12 sqlmap.py ...
- iOS开发--常用技巧 (MJRefresh详解)
iOS开发--常用技巧 (MJRefresh详解) https://github.com/CoderMJLee/MJRefresh 下拉刷新01-默认 self.tableView.head ...
- Android之ListView常用技巧
ListView是一个非常常用的列表控件,虽然在5.x时代ListView的风头正在逐渐的被RecyclerView抢去,但是ListView的使用范围依然十分广泛. 接下来的ListView的常用技 ...
- 【读书笔记《Android游戏编程之从零开始》】7.Android 游戏开发常用的系统控件(Dialog)
在Android应用开发中,Dialog(对话框)创建简单且易于管理因而经常用到,对话框默认样式类似创建样式的Activity.首先介绍android.app.AlertDialog下的Builder ...
- 【读书笔记《Android游戏编程之从零开始》】5.Android 游戏开发常用的系统控件(ProgressBar、Seekbar)
3.7 ProgressBar ProgressBar类官方文档地址:http://developer.android.com/reference/android/widget/ProgressBar ...
- Android App开发常用专题开源代码
Android App开发中用到过的专题类开源代码: 项目的需求多了,不知不觉成了Github摘抄员,感谢分享精神,节省了很多弯路和时间.不过想要实现指定效果,还是要看懂作者的思路才好下手改造. 主题 ...
- Android开发学习之路-Android Studio开发小技巧
上一次发过了一个介绍Studio的,这里再发一个补充下. 我们都知道,Android Studio的功能是非常强大的,也是很智能的.如果有人告诉你学Android开发要用命令行,你可以告诉他Andro ...
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...
随机推荐
- 设计院老师良心汇总:值得牢记的15个CAD基础技巧,能帮大忙
哈喽!你们的CAD魔鬼(老师)来喽! 良心CAD技巧汇总,设计院师傅精心汇总,值得你牢记的15个CAD基础技巧,满满的都是干货,日常最常见的问题以及解决方法这里都汇总给你,给你高效的绘图体验,关键时刻 ...
- [转]RPA认证 Developer UIPath Certificate,细说uipath认证学习,Online Quiz和Practical Exam项目详解
本文转自:https://blog.csdn.net/u010369735/article/details/88621195 UIPath,RPA里算是比较简单易操作的一款软件了,因为公司业务的需要, ...
- angluarjs实现过滤并替换关键字
html样式 <body ng-app="myapp" ng-controller="myCtrl"> <input type="t ...
- 【转载】C++编译过程
C++编译过程 C++ 编译过程在介绍编译器之前,先简单地说一下 C++ 的编译过程,以便理解编译器的工作.编译(compiling)并不意味着只创建仅仅一个可执行文件.创建一个可执行文件是一个多级过 ...
- Python入门基础学习(文件与异常处理)
Python基础学习笔记(七) 捕获异常的语法格式: 文件的基本操作: 打开文件 读.写文件 关闭文件 read方法 --读取文件: open函数的第一个参数是要打开的文件名(文件名区分大小写) 如果 ...
- 《2018:skymind.ai 发布了一份非常全面的开源数据集》
这是一份非常全面的开源数据集,你,真的不想要吗? 近期,skymind.ai 发布了一份非常全面的开源数据集.内容包括生物识别.自然图像以及深度学习图像等数据集,现机器之心将其整理如下:(内附链接 ...
- JavaScript中随机打乱一个数组
JavaScript中随机打乱一个数组 function shuffle(arr) { let i = arr.length; while (i) { let j = Math.floor(Math. ...
- UOJ #450. 【集训队作业2018】复读机
前置知识单位根反演自己去浅谈单位根反演看(此外可能需要一定的生成函数的姿势) 首先一看\(d\)这么小,那我们来分类讨论一下吧 当\(d=1\)时,显然答案就是\(k^n\) 当\(d=2\)时,如果 ...
- 推荐书单(网课)-人生/编程/Python/机器学习-130本
目录 总计(130本) 一.在读 二.将读 三.已读 非专业书单(77本) 四.已读 专业书单(53本) 五.已看网课(8个) 六.在看网课 一个人如果抱着义务的意识去读书,便不了解读书的艺术.--林 ...
- [开源] FreeSql 配套工具,基于 Razor 模板实现最高兼容的生成器
FreeSql 经过半年的开发和坚持维护,在 0.6.x 版本中完成了几大重要事件: 1.按小包拆分,每个数据库实现为单独 dll: 2.实现 .net framework 4.5 支持: 3.同时支 ...