I'm writing an Android app that accompanies a website and I wanted to make the app intercept certain URLs. I found multiple examples through Google and on SO but none of them work. I create my Intent:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://xgn.nl/article/" + String.valueOf(id)));
and then launch it from another Activity (where i is the intent from above)
startActivity(i);
The intent filter for the activity that should receive this is:
<activity android:name=".ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" android:mimeType="text/*" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
I also tried it without the mimeType in the filter. The intent resolver sees the filter but complains that the data does not match with the data in the intent and launches the browser instead.
What am I doing wrong?
I actually solved it 5s after I posted it, the problem was that you need to specify the fully qualified class name for this to work :)
<activity android:name="nl.xgn.ArticleActivity" android:theme="#style/XGN.Red">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" android:host="xgn.nl" android:pathPrefix="/article/" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
EDIT: To be more precise, the error I was seeing in logcat was wrong. The data type did match, but it chose the more visible browser class over my lazily defined activity. Specifying the full class name and removing the mimeType made this work.
Related
In my android application, I have registered for intent filter android.intent.action.SEND in one of my activities to receive data from other applications.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<data android:mimeType="application/pdf" />
<data android:mimeType="text/x-vcard" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="application/pdf" />
</intent-filter>
When data is shared from other apps with my app using share intent, this activity receives the intent. I would like to know the package name of app which shared the data with my app.
I tried following code but it always returns null.
final String packageName = this.getCallingPackage()
Is there any way I can get the package name of app that shared data with my app using intent that i am receiving?
If the share intent was created using ShareCompat.IntentBuilder it will contain the extra EXTRA_CALLING_PACKAGE.
But most apps don't provide this information. And if the extra is present you should not use it for any security-critical functionality because senders are free to provide any value they like.
To use getCallingPackage(), the caller should use startActivityForResult(Intent, int) instead of startActivity(). Otherwise, it will return null. Straight from the doc:
Note: if the calling activity is not expecting a result (that is it
did not use the startActivityForResult(Intent, int) form that includes
a request code), then the calling package will be null.
https://developer.android.com/reference/android/app/Activity.html#getCallingPackage%28%29
From the research on NFC and opening applications I understand that they are capable of opening an app. My question is can it open the app to a specific product page?
Like if I am at a store and I have an nfc tag placed in front of two products can I program each tag to open the app and go to the specific page for each product that would contain information about that particular product?
Thanks!
Let's assume you have a tag containing the URI http://www.example.com/myproduct1. Then you could register your product-specific activity "MyProduct1Activity" with the following intent filter:
<activity android:name=".MyProduct1Activity">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/myproduct1" />
</intent-filter>
</activity>
This will cause the activity MyProduct1Activity to be started for tags that contain the URI http://www.example.com/myproduct1.
Alternatively, if multiple products share one activity, you could register a more generic intent filter:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/" />
</intent-filter>
And retieve the intent within your activity using the activity's getIntent() method to parse the URI for the specific product.
Although I have gone through related document on google for app indexing
https://developers.google.com/app-indexing/webmasters/details
But still have confusion that If I want to receive incoming data on launcher activity and from there If can take control and start relevant activity/fragment depending on some internal parsing over incoming url.
You have to declare the action, category and data in a separate <intent-filter> tag, not in the same one with launcher specs.
<activity>
<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" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="your.domain.com"
android:scheme="http" />
</intent-filter>
</activity>
Not quite sure if I understood what your asking, so please correct me if I'm not answering your question.
Only the activity (or activities) that you put the intent filter in will catch an intent and start. Therefore, if you only put the intent filter in one activity, only that activity will start, and not any of the others. You can put multiple intent filters in the same activity to catch multiple intents. You can also use the path segment of the url to send more information to your activity, and parse it in your activity.
Put the following in your manifest under the activity you want to launch (the path is optional):
<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="http" android:host="www.yourwebsite.com" />
</intent-filter>
Use a url like http://www.yourwebsite.com/yourstring to open the app.
Then use getIntent().getData() to get the uri that started the activity. You can then parse the uri to get yourstring.
Add Following Intent filter to Activity you want to open via Deep linking in Manifest.
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Then As per Intent data you get in Activity you can perform your action, too.
The web and stackoverflow contain several examples how to get a file from another Android app (e.g., to use it as email attachment) using an ACTION_GET_CONTENT intent. But what kind of class do I have to implement to create an application providing content for the ACTION_GET_CONTENT event such as I can choose this app (e.g., for selecting an email attachment).
Is a ContentProvider the right solution? And what do I have to add to my AndroidManifest.xml?
After some hours of web search I found the following solution.
Implement an Activity handling intents. Within, use the following or more specific code:
Uri resultUri = // the thing to return
Intent result = new Intent();
result.setData(resultUri);
setResult(Activity.RESULT_OK, result);
finish();
Add the following to the Manifest:
<activity
android:name="ActivityName"
android:label="Some label" >
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.OPENABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
starting from api level 18 incoming intent can also have EXTRA_ALLOW_MULTIPLE set to true and in this case you can send back in result more than one file. To do so you need to set it as ClipData:
resultIntent.setClipData(clipData)
I'm trying to figure out how to launch an activity in my app from a custom URI such as myapp://myuriactivity
I've read a lot about the intent and intent filters in the android references and also read several examples, but for some reason I can't get my simple test to work. Below is my manifest file, can anyone tell me what I'm doing wrong? With the below file, if I open the browser and try to navigate to http://org.test.launchtest it just says the page doesn't exist. Shouldn't this work?
<?xml version="1.0" encoding="utf-8"?>
<activity android:name=".MyUriActivity">
<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="http" android:host="org.test.launchtest" />
Hmm I wasn't able to do the trick with host too. Hence I ended up with using more specific scheme.
<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="com.stackoverflow.example" />
</intent-filter>
Probably you may use scheme solution too?