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