在系统里设置文件默认打开APP
来源:互联网 发布:财务评价软件 编辑:程序博客网 时间:2024/06/05 19:18
在系统里设置文件默认打开APP
public boolean setPreferredActivity(String packName,String clsName,Uri uri,String type){ PackageManager packageManager = mContext.getPackageManager(); String str1 = "android.intent.category.DEFAULT"; String str3 = "android.intent.action.VIEW"; // 设置默认项的必须参数之一,用户的操作符合该过滤器时,默认设置起效 IntentFilter filter = new IntentFilter(str3); filter.addCategory(str1); try { filter.addDataType(type); } catch (MalformedMimeTypeException e) { // TODO Auto-generated catch block e.printStackTrace(); } ComponentName component = new ComponentName(packName, clsName); Intent intent = new Intent(str3); intent.addCategory(str1); intent.setDataAndType(uri, type); List<ResolveInfo> resolveInfoList = packageManager .queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS); int size = resolveInfoList.size(); ComponentName[] arrayOfComponentName = new ComponentName[size]; int bestMatch = -1; for (int i = 0; i < size; i++) { ActivityInfo activityInfo = resolveInfoList.get(i).activityInfo; String packageName = activityInfo.packageName; String className = activityInfo.name; int uid = resolveInfoList.get(i).activityInfo.applicationInfo.uid; int match = resolveInfoList.get(i).match; Log.e(Tag, packageName+"/"+className+"/"+uid+"/"+match); if(packageName.equals(packName)){ bestMatch = match; } // 清除之前的默认设置 packageManager.clearPackagePreferredActivities(packageName); ComponentName componentName = new ComponentName(packageName, className); arrayOfComponentName[i] = componentName; } if(bestMatch<0){ packageManager.addPreferredActivity( filter, IntentFilter.MATCH_CATEGORY_SCHEME, arrayOfComponentName, component, UserHandle.getCallingUserId());//解决第一次调用不生效问题 return false; }else{ packageManager.addPreferredActivity(filter, bestMatch, arrayOfComponentName, component,UserHandle.getCallingUserId()); return true; } }这是设置默认的视频播放器 public boolean setPreferredActivityOfVideo(String packName,String clsName){ boolean result = false; Cursor cur = mContext.getContentResolver().query( Media.EXTERNAL_CONTENT_URI, null, null, null, null); int pathIndex = cur.getColumnIndex(Media.DATA); if(cur.moveToFirst()){ String path = cur.getString(pathIndex); Uri uri = Uri.fromFile(new File(path)); result = setPreferredActivity(packName, clsName, uri, "video/*"); } cur.close(); return result; }
记得 <uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS" /> 及系统签名噢
0 0
- 在系统里设置文件默认打开APP
- Android-在系统设置里默认always显示开发者选项
- 设置文件默认打开方式
- 设置文件默认打开方式
- AIR 用系统默认应用打开文件 Open with default App
- 设置app为系统默认字体
- VS2005 CFileDialog设置文件默认打开路径
- Eclipse 设置文件的默认打开方式
- Eclipse 设置文件的默认打开方式
- Eclipse设置文件默认打开方式
- eclipse myeclipse 设置文件默认打开方式
- Linux 设置文件默认打开方式
- Eclipse 设置文件的默认打开方式
- myeclipse 文件默认打开方式设置
- Eclipse设置文件默认打开方式
- ubuntu 设置打开文件的默认方式
- Eclipse设置文件默认打开方式
- eclipse设置文件默认打开方式
- NGUI、UGUI、粒子间的渲染顺序解决方案
- NC工具的使用说明教程
- 51nod 2级算法题-1095
- 第1章信号完整性分析概论----基本定义和四类噪声问题
- 第五次上机作业
- 在系统里设置文件默认打开APP
- OpenGL鼠标交互
- CodeForces
- ES6 Promise 对象理解
- 用maven创建一个Web project
- C++编码规范------命名约定
- Android-实现背景渐变动画
- bzoj3306 树
- 开机自启动(整合)