博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Activity 常用技巧
阅读量:7078 次
发布时间:2019-06-28

本文共 3391 字,大约阅读时间需要 11 分钟。

 

 

  

1.设置 Activity 背景色为透明

在style.xml里面声明:

在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. 判断当前线程是否是主线程

有如下三个方式:

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();}

 

转载地址:http://wtpml.baihongyu.com/

你可能感兴趣的文章
“3.15”曝二维码安全漏洞 瑞星呼吁重视WiFi安全
查看>>
关于团队建设的访谈
查看>>
Openstack:一根中国稻草-【中国IC微专栏】2016.4.28
查看>>
Lync 小技巧-51-Lync 2013-不加域-客户端-1-下载-证书-信任链
查看>>
安装Exchange Server 2013
查看>>
Win7部署基础知识(1):部署的基本概念与方法论
查看>>
州小吃”小程序正式发布,酷客多全程技术支持
查看>>
理解思科IPS系统的virtual sensor
查看>>
MySQL5.6创建用户密码不再明文显示在binlog二进制日志文件里
查看>>
Lync 小技巧-28-同一环境同一客户端不同的结果
查看>>
正在变味的网络红包
查看>>
共享汽车不是一盘好生意,入坑需谨慎!
查看>>
***组网不用愁之2-总部路由器配置
查看>>
使用“一键清理”功能要防止删除WORD文档
查看>>
SVN常用命令
查看>>
内存模型
查看>>
【C++】区分接口继承和实现继承
查看>>
转 jQuery插件Highcharts、flexigrid实践
查看>>
Windows Phone 8 SDK RC 版推出
查看>>
Database2Sharp代码生成工具使用心得
查看>>