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 ...
随机推荐
- Sqlite-net 修改版 支持中文和CodeFirst技术
最近, 做的一个windows 桌面WPF程序, 需要数据库支持.尝试了 sql server 的开发版,使用EF , 效率太低.后来采用sqlite数据库,中间踩坑无数.但最终完美的解决了这些问题. ...
- WPF TreeView 虚拟化-设置滚动到选中项
前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数 ...
- Git - Git推送本地分支到远程分支报错(! [rejected] non-fast-forward)的解决办法
一般都是冲突造成的,解决方案执行如下命令(dev为分支名称): git fetch origin dev #获取远程 dev 分支的修改 git merge origin dev #合并 ...
- Assign a Standard Image 设置图标
eXpressApp Framework (XAF) includes standard images embedded into the DevExpress.Images assembly. In ...
- JS基础语法---Date对象---格式化日期
格式化后的指定格式的日期和时间,封装一个函数 function getDate() { var dt = new Date(); var year = dt.getFullYear(); var mo ...
- JS基础语法---预解析
预解析:就是在解析代码之前 1. 预解析做什么事? 把变量的声明提前了----提前到当前所在的作用域的最上面 函数的声明也会被提前---提前到当前所在的作用域的最上面 举例: function ...
- eclipse git 主干代码合并到分支
https://blog.csdn.net/wwd0501/article/details/80676807 eclipse git 主干代码合并到分支: 1.项目切换至分支: 2.选中项目右键--& ...
- Linux:LNMP环境的搭建
LNMP环境的搭建 安装DNS服务器 安装DNS服务 yum install bind -y DNS的配置 创建正向解析 以创建一个名为"lsy.com"的正向查找区域为例: 第一 ...
- 学习postman教程
postman可以做什么 1.可以做单接口的测试 2.可以调试接口 3.对接口设置变量后,可以做多接口的测试,并输出报告 如何用postman调试接口 1.填写api地址 2.选择请求方式 3.输入a ...
- 201871010136-赵艳强《面向对象程序设计(java)》第一周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/ ...