【问题标题】:How to set file or url "open with" programmatically? [duplicate]如何以编程方式设置文件或网址“打开方式”? [复制]
【发布时间】:2018-07-29 16:50:33
【问题描述】:

当文件或 url 打开时,Android 会询问用户如何打开。我的应用程序需要设置此选项。如果没有解决方案,我无法完成申请。请告诉我该怎么做。

【问题讨论】:

    标签: android android-file


    【解决方案1】:

    如果您与Intent 共享文件,您可以先创建您的Intent openWith 对象(但不要运行它)。然后像这样列出所有可用的活动:

    PackageManager pm = (Activity)this.getPackageManager();
    List<ResolveInfo> resInfo = pm.queryIntentActivities(openWith, 0);
    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        String label = (String) ri.loadLabel(pm);
        String activity = ri.activityInfo.name;
        // choose needed packageName and activity here
    }
    

    然后将选定的 packageName 和活动对添加到“打开方式”操作的意图中:

    intent.setComponent(new ComponentName(packageName, activity));
    

    并运行它。

    【讨论】:

      【解决方案2】:

      您想在您的应用中实现打开文件/网址的能力,对吧? 这是文档https://developer.android.com/training/basics/intents/filters.html 所以你的“查看者”活动必须有这样的意图过滤器

      <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT"/>
          <data android:mimeType="*/*" />
      </intent-filter>
      <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT"/>
          <data android:scheme="http|https" />
      </intent-filter>
      

      【讨论】:

      • 没有。使用谷歌驱动器。
      猜你喜欢
      • 1970-01-01
      • 2010-12-12
      • 2015-11-27
      • 2013-10-31
      • 2013-02-19
      • 2017-08-21
      • 2011-08-12
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多