1:在android6.0之后使用WindowManager需要使用者认真同意跳入一个界面 参考博客:https://blog.csdn.net/chenlove1/artic
参考博客:https://blog.csdn.net/chenlove1/article/details/52047105
代码解决:
1、
6.0 之前添加权限即可使用WindowManager
2、6.0+之后: 就是跳过去把这个开关打开
// Android6.0+ WindowManager使用
if (Build.VERSioN.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(WindosManagerActivity.this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 10);
Log.e("denganzhi","onCreate->开关没有打开");
}else{
Log.e("denganzhi","onCreate->开关已经打开,可以干活了");
}
}
@Targetapi(23)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
Log.e("denganzhi","权限没有通过");
}else{
Log.e("denganzhi","权限通过");
}
}
}
具体界面:
2. FileProvider适配 拍照、启动App安装Https://blog.csdn.net/lmj623565791/article/details/72859156
大神博客写的大概意思是:
Android7.0 禁止应用向外部使用file:// URI 共享文件
解决: 使用content:// URI 指向文件, 并且给URL 临时访问权限,通过FileProvider实现临时访问权限
1. 读写目录权限动态申请:
private final int SDK_PERMISSION_REQUEST = 127;
// 权限动态申请
ArrayList permissions = new ArrayList();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissions.size() > 0) {
requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
}
}
// 是否允许
@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
// TODO Auto-generated method stub
if (requestCode == SDK_PERMISSION_REQUEST)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED )
{
Toast.makeText(FileProviderActivity.this,"权限通过:"+grantResults.length, Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
2. 配置FileProvider
2.1.AndroidManifest.xml 中配置FileProvider 组件:
2.2.在res/xml创建文件夹配置file_paths.xml 文件
实现拍照功能:
ImageView mIvPhoto;
// android Api 调用
public void callPhoto(View view){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
String filename = new SimpleDateFORMat("yyyyMMdd-HHmmss", Locale.CHINA)
.format(new Date()) + ".png";
File file = new File(Environment.getExternalStorageDirectory()+"/1moviestest/", filename);
mCurrentPhotoPath = file.getAbsolutePath();
Uri fileUri = null;
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(this, "com.denganzhi.permission", file);
} else {
fileUri = Uri.fromFile(file);
}
// Uri fileUri = FileProvider.getUriForFile(this, "com.zhy.android7.fileprovider", file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// mCurrentPhotoPath 拍照的图片路径,使用imageView显示出来
Log.e("currentPath:",mCurrentPhotoPath);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TAKE_PHOTO) {
mIvPhoto.setImageBitmap(BitmapFactory.decodeFile(mCurrentPhotoPath));
}
// else tip?
}
Apk启动安装功能
// 启动安装
public void apkInstallMethod(View view){
//File file = new File(Environment.getExternalStorageDirectory(), "actionbar.apk");
File file = new File(getFilesDir(),"actionbar.apk");
Log.e("denganzhi1",file.getAbsolutePath());
if(file.exists()==false){
copyDB("actionbar.apk"); // 把assert下actionbar.apk 拷贝的 /data/data/packageName/file 下 , 没有安装包
Toast.makeText(ApkInstallActivity.this,"文件不存在",Toast.LENGTH_SHORT).show();
return;
}
Uri fileUri = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// 这里对应AndroidManifest.xml 配置authorities com.denganzhi.permission
fileUri = FileProvider.getUriForFile(this, "com.denganzhi.permission", file);
} else {
fileUri = Uri.fromFile(file);
}
intent.setDataAndType(fileUri,
"application/vnd.android.package-arcHive");
startActivity(intent);
}
private void copyDB(final String dbname) {
new Thread(){
public void run() {
try {
File file = new File(getFilesDir(),dbname);
if(file.exists()&&file.length()>0){
Log.e("denganzhi1","数据库是存在的。无需拷贝");
return ;
}
InputStream is = getAssets().open(dbname);
FileOutputStream fos = openFileOutput(dbname, MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
--结束END--
本文标题: Android 6.0+ 项目开发中用到适配功能
本文链接: https://lsjlt.com/news/28994.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0