I want to associate files of particular extension (say, any file with the extension .xyz) with my app. This means, when user taps SaveFile1.xyz in File explorer (or other places, like gmail, drive etc.), my app should launch and the Uri of the file will be passed in an intent to my activity.
While this is the expected behavior, I don't know how to associate custom files. I have read and understood how intent matching works and the possible values in data tag, but it didn't work. I have checked some stackoverflow answers (Ex, this and this), but they use file scheme and pathPatterns.
We cannot use the pathPattern because we get a content uri (Uri with scheme=content), which is the abstracted path and will not have any extensions.
The mime type setting can be used only for the predefined values (like image/png can associate all png images with the app).
I could set the mime type as "*/*", but I don't want my app to accept every file (and then reject it in onCreate or something).
Has anyone solved this problem? Any help will be greatly appreciated.
I have the following in the activity tag of the manifest file.
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<!--<data android:mimeType="application/pdf"/>
<data android:mimeType="image/png"/>-->
<!--<data android:scheme="content" android:mimeType="*/*"/>-->
</intent-filter>
</activity>
Edit1:
I set the mime type as "*/*" and added the following line in Activity.onCreate to see the type in the intent (and set it in the manifest file) ...
Intent dataIntent = getIntent();
Log.i(TAG, "dataIntent.getType() = " + dataIntent.getType());
.... but got the following as output.
dataIntent.getType() =
Its not even null.
Related
I'm doing an NFC Application and was wondering if this scenario is possible:
Say, I have 2 NFC tags and 2 activities in one project.
NFC A is written to open up Activity A by writing MIME type in NFC A as
application/com.example.hello
In the project's manifest file, Activity A has this intent filter:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data android:mimeType="application/com.example.hello" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
So, this works perfectly fine.
I'm going to add another NFC Tag, and another activity.
NFC B is written to open up Activity B
Now, how should I write my MIME type into NFC B and set up the intent-filter for Activity B? Considering Activity A and Activity B are both in one project and package.
If I write the same MIME type in NFC A and B for Activity A and B, I will be asked which activity to open upon tapping and I don't want that.
One way might be to have two MimeTypes in your manifest. Then you have those MimeTypes linked to an (additional) activity which will check which MimeType is actually on your tag (A or B). Depending on what you find you can lauch the respective activity A or B.
Depending on what you want to achieve, the easiest way would be to use two tags with two different record types (e.g. two different MIME types, but note that you should prefer to use NFC Forum external type names over custom MIME types!)
Assuming you have
Tag A:
+--------------------------------------+
| MIME:application/com.example.hello.a |
+--------------------------------------+
Tag B:
+--------------------------------------+
| MIME:application/com.example.hello.b |
+--------------------------------------+
Then you can define intent filters for your activities, so that ActivityA will only be triggered by tag A and ActivityB will only be triggered by tag B:
<activity android:name=".ActivityA" ...>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.example.hello.a" />
</intent-filter>
</activity>
<activity android:name=".ActivityB" ...>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/com.example.hello.b" />
</intent-filter>
</activity>
I am developing an application for searching for places. In the application there is a button where user can share a specific place with other user. I was thinking about sharing a link that when other user, "the invited one", clicks on it, my application opens on the shared place page. Just like Clash Royale game, if you know it. The idea is that. How can I do so? Does anyone have idea how to implement this?
Thanks in advance.
To achieve this you need to Create Deep Links to App Content
Code would be something like this:
1) Add intent filter in your activity
<activity android:name="...">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="example.com"
android:scheme="schemeName" />
</intent-filter>
</activity>
2) Get data in you activity
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
3) Create sharable URL
eg - http://example.com?data=<your data to share>
4) Now you need to write some script for your page (http://example.com)
that will receive your header data and redirect to open app.
for this get some idea from here
* This is not a complete code but hope will help you.
In my app manifest i wrote this code to open some links with my app adding the intent-filter:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="example.com" />
</intent-filter>
I tried to send via email to myself a link; for example: http://www.example.com/1234. click over the link i open the app.. That's great. Now, is it possible take the 1234 part of the link and put it into a textview? Something like:
mTextView.setText(textFromLink);
thanks
Did you already get access to the URL inside your app? If not, you can get it e.g. in your onCreate method with
Uri link = getIntent().getData();
which returns a Uri object:
http://developer.android.com/reference/android/net/Uri.html
This object has several methods to get the specific parts of the URL. In your case it will probably be
String content = link.getLastPathSegment():
http://developer.android.com/reference/android/net/Uri.html#getLastPathSegment()
This returns a string that you can set in your TextView:
mTextView.setText(content);
I am new in android development, I want to build a music player but the problem in front of me is how do i make my app open from gallery.
For example,
if we want to open any music file then we select it, the the android mobile ask which player do we want to use.
so how can I add my app in that option.
please help.
If you want your app to be in the list for opening an audio file, you need to tell the system your app can open those files. You can do that by adding something like the following to your activity tag in your AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
Intent filters are part of the core principles in Android development. You should try to get some knowledge on these basic topics before getting started.
It helps you
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Actually you can open activity not app at self just simply add action intent filter in mainfest like this
<activity class=".foo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="com.me.love" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
other app code :
Intent intent = new Intent("com.me.love");
startActivity(intent);
however if you want to tell android system that your activity can handle some actions like share or send data you just have to add "send" action to your activity in mainfest like so :
<activity class=".boo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
so now when ever user click share button android system will check all activitys that have action string "android.intent.action.SEND" then give the user list of activitys that have send action and if you did added same action as boo activity dose to your activity then it will be one of chooses in the list.
I want to add a feature to my app that when I touch the number in a message, the user can decide to send the number to my app or Android Dialer.
For example my friend send me a code and i want to use this code for special ussd code that my app run it.
I think I have to use implicit intent but I don't know how?
Thanks
I have quoted the below links
-intent filter
-Intents implicit\explicit
Implicit intents specify the action which should be performed and
optionally data which provides data for the action.
For example the following tells the Android system to view a webpage.
All installed web browsers should be registered to the corresponding
intent data via an intent filter.
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.vogella.com")); startActivity(i);
If an Explicit intent is send to the Android system, it searches for all
components which are registered for the specific action and the
fitting data type.
If only one component is found, Android starts this component
directly. If several components are identifier by the Android system,
the user will get an selection dialog and can decide which component
should be used for the intent.
How to use
You can register your own components via Intent filters. If a
component does not define one, it can only be called by explicit
intent.
Register an activity as Browser
<activity android:name=".BrowserActivitiy"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>
UPDATES
A code sample for mimeType
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
I assume you are talking about a number in a SMS that you received. On clicking that number you want the user to get an option to either dial or use your app.
If yes then you need to include intent filter in your manifest file for launcher activity.