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