0

I'm quite new to android programming and I'm also not the best programmer and the following code should work based on my knowledge... So, that's the issue: I want my application to be shown as an option for opening a txt-file on my phone. That's why I created a broadcast receiver, which is specified in my manifest like this:

<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>

And my broadcastreceiver class looks like this:

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);
    }
}

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Felix
  • 9
  • 1
  • 8

1 Answers1

1

That's why I created a broadcast receiver

A BroadcastReceiver is not used to open documents this way. An Activity does that.

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

That is because other apps will use startActivity(), not sendBroadcast(), with ACTION_VIEW Intents.

Create an activity and use your <intent-filter> with it. It should work with a few apps, though not very many. Eliminating <data android:pathPattern=".*\\.txt"/> and <data android:host="*"/>, and adding <data android:scheme="content"/>, will help increase compatibility.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Worked, for more information [link](http://stackoverflow.com/questions/11152838/why-isnt-my-app-on-the-list-of-apps-to-open-txt-file) – Felix Apr 01 '17 at 14:16