Phone 没有显示我打开 txt 文件的应用程序

Phone doesn't show my application to open txt-files

我对 android 编程还很陌生,我也不是最好的程序员,根据我的知识,下面的代码应该可以工作......所以,这就是问题所在: 我希望我的应用程序显示为在 phone 上打开 txt 文件的选项。这就是我创建广播接收器的原因,它在我的清单中指定如下:

<receiver android:name="MyBroadcastReceiver" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_VIEW"/>
                <action android:name="android.intent.action.ACTION_EDIT"/>
                <action android:name="android.intent.action.ACTION_PICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data android:scheme="file"/>
                <data android:mimeType="text/plain"/>
                <data android:pathPattern=".*\.txt"/>
                <data android:host="*"/>
            </intent-filter>
        </receiver>

我的广播接收器 class 看起来像这样:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intentNoteActivity = new Intent(context, NoteActivity.class);
        intentNoteActivity.putExtra("URI", intent.getData());
        context.startActivity(intentNoteActivity);
    }
}

但是,如果我尝试在 phone 上打开应用程序安装位置的 txt 文件,它不会显示我的应用程序。我做错了什么?

That's why I created a broadcast receiver

A BroadcastReceiver不习惯用这种方式打开文档。 Activity 做到了。

But if I try to open a txt-file on my phone on where the application is installed it does not show my application.

那是因为其他应用将使用 startActivity(),而不是 sendBroadcast()ACTION_VIEW Intents

创建一个 activity 并使用你的 <intent-filter>。它应该适用于一些应用程序,但不是很多。删除 <data android:pathPattern=".*\.txt"/><data android:host="*"/>,并添加 <data android:scheme="content"/>,将有助于提高兼容性。