Android Activity 常用技巧

本文介绍Android Activity开发中的实用技巧,包括设置背景透明、安全退出应用、显示与隐藏输入法、动态权限请求等,并提供了详细的代码示例。
Android Activity 常用技巧
Android Activity 启动模式和任务栈
Android 页面跳转之生命周期调用顺序问题

 

 

  

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

有如下三个方式:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值