如何使用 mod 文件(.mcpack)向 MinecraftPE 发送 Android 意图?

huangapple go评论66阅读模式
英文:

How to send android intent to MinecraftPE with mod file (.mcpack)?

问题

我想编写一个简单的Minecraft PE模组加载器(Java + Android Studio),但是遇到了一个问题。
我以为我只需要将mod文件从我的应用资源复制到"/games/com.mojang/behaviour_pack/",但是Minecraft没有发现它!
然后我从应用商店下载了一些模组加载器,以便有一个示例,注意到这个加载器在安装一些模组后会打开Minecraft,之后游戏会请求权限来管理我的存储空间。

所以,我理解我需要向Minecraft发送一些带有mod文件的意图(intent),然后它会自行进行管理。
我已经尝试过的代码:

    String filepath = "android.resource://" + c.getPackageName() + "/" + filename;
    
    try {
        Uri uri = Uri.parse(filepath);
        // 也尝试过ACTION_SEND
        Intent intent = new Intent(Intent.ACTION_MAIN);
        String mime = c.getContentResolver().getType(uri);
        intent.setDataAndType(uri, mime);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        PackageManager packageManager = c.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        Log.i("Acivities:", activities.toString());
        // 'c' - Context
        c.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }

可用于打开mod文件的活动列表为空,因此它会崩溃。
我该如何解决这个问题?
我在意图(intent)方面做错了什么?

英文:

I want to write a simple Minecraft PE mods loader (Java + Android Studio), but encoutered a problem.<br>
I thought that all i need is to copy mod file from my app resources to "/games/com.mojang/behaviour_pack/", but minecraft didn't see it!
Then i downloaded some mod loader from play market to have an example and noticed that this loader opens Minecraft after installing some mod, and after that, the game asks permission to manage my storage.


So, as I understand, i need to send some intent to minecraft with mod file, and it will manage further by itself.
Code that i've already tried:


    String filepath = &quot;android.resource://&quot; + c.getPackageName() + &quot;/&quot; + filename;
    
            try {
                Uri uri = Uri.parse(filepath);
                //Tried also ACTION_SEND
                Intent intent = new Intent(Intent.ACTION_MAIN);
                String mime = c.getContentResolver().getType(uri);
                intent.setDataAndType(uri, mime);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
                PackageManager packageManager = c.getPackageManager();
                List&lt;ResolveInfo&gt; activities = packageManager.queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
                Log.i(&quot;Acivities:&quot;, activities.toString());
                // &#39;c&#39; - Context
                c.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }

List of avaliable activities to open mod file is null, so it just crashes.<br>
How can i solve this thing??<br>
What am i doing wrong with this intent?

P.S. Sorry for my bad english.

答案1

得分: 1

答案是将修改后的文件夹简单地复制到外部存储的任何文件夹中,并发送正确的意图,如下所示:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("file/*");
intent.setData(Uri.parse("minecraft://?import=" + filepath));
c.startActivity(intent);

其中 'c' - 上下文,"filepath" - 外部存储中文件的路径。

英文:

The answer is to simply copy mods to any folder on external storage and send a right intent, shown below:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType(&quot;file/*&quot;);
intent.setData(Uri.parse(&quot;minecraft://?import=&quot; + filepath));
c.startActivity(intent);

where 'c' - Context, "filepath" - path to file in external storage.

huangapple
  • 本文由 发表于 2020年10月22日 21:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64483146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定