Taking uri data through intent in android - android

Hi i have to do connectivity between android app and browser. So while clicking a button on browser it should redirect to android app. in android activity i have written
Uri data = getIntent().getData();
if (data.equals(null)) {
System.out.println("Data is null");
} else {
String scheme = data.getScheme();
System.out.println(scheme);
String host = data.getHost();
int port = data.getPort();
List<String> params = data.getPathSegments();
String first = params.get(0); // "hello"
System.out.println(first);
and in manifest i have already given
<intent-filter>
<data android:scheme="Integration" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in html on button click i have given <FORM METHOD="LINK" ACTION="Integration://1">
it is throwing a indexoutofboundexception.
Please tell me where is the mistake
Updated
*I was unnecessarily using intent in an activity. By removing that n parameter in html5 my app is running successfully now.*

Quoting answer from: How to listen for a custom URI
To register a protocol in your android app, add an extra block to the AndroidManifest.xml
I modified the code a little, but thought I'd quote the source too
<manifest>
<application>
<activity android:name=".activityToCall">
<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="scheme" android:host="path"/>
</intent-filter>
</activity>
</application>
</manifest>
Then when a url matching your schema is opened your app will be called to handle it I assume.
Taking into consideration that scheme and pathcorrespond to this:
scheme://host/path
After that, in the activity you've set in the manifest to handle this stuff:
Uri data = getIntent().getData();
if (!data.equals(null)){
String scheme = data.getScheme();
//Or whatever you needed
}

Related

How to launch a third-party app from their URL with an SMS on Android?

I've been thinking about using AWS's Simple Notification Service (SNS) to send texts. I'm making something that sends a text message to arm SimpliSafe when it thinks that I've left the house. I want to make the link so if someone clicks on it, it opens the SimpliSafe app. The only auto-links that apply are for web addresses and other things, though, so I can't use an intent:///. It looks like the URL https://simplisafe.com/sn/..* will launch an activity as in the following:
<intent-filter
android:autoVerify="true">
<data
android:scheme="https"
android:host="simplisafe.com"
android:pathPattern="/sn/..*"/>
<category
android:name="android.intent.category.BROWSABLE"/>
<category
android:name="android.intent.category.DEFAULT"/>
<action
android:name="android.intent.action.VIEW"/>
</intent-filter>
I don't understand what this means, though. Is this regex? How do I trigger this? So far, if I make a sample request to https://simplisafe.com/sn/..*, or anything instead of the ..*, with curl it gives me a 302 Moved Temporarily error. If I make a sample request to https://simplisafe.com/sn/index.html with curl it gives me a 404 Not Found error. If I open this URL from my browser, just like it would open from a text, it opens the Google Play store. How do I make it open the app instead of the Play Store?
It turns out that when I launch it from the browser it doesn't do a lookup of the available apps first. The SimpliSafe page is designed to handle this, so it knows that the best option is to send me to the Play Store. If I had actually tested it with a text message, then I would have found out that it opens the SimpliSafe app.
First, configure it in AndroidManifest.xml, add an intent-filter to the corresponding Activity, as follows:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<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:scheme="paraches" />
</intent-filter>
</activity>
</application>
Next, add the relevant parsing code to the corresponding Activity, as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri uri = intent.getData();
if (uri != null) {
String host = uri.getHost();
String dataString = intent.getDataString();
String id = uri.getQueryParameter("id");
String path = uri.getPath();
String path1 = uri.getEncodedPath();
String queryString = uri.getQuery();
Log.d("Alex", "host:"+host);
Log.d("Alex", "dataString:" + dataString);
Log.d("Alex", "id:" + id);
Log.d("Alex", "path:" + path);
Log.d("Alex", "path1:" + path1);
Log.d("Alex", "queryString:" + queryString);
}
}
}
Look at the Log, as follows:
host:schemedemo
dataString:paraches://schemedemo/get/info?id=10000
id:10000
path:/get/info
path1:/get/info
queryString:id=10000

How to use Deep-linking?

i am working on Deep-link from an Android application to connect my Android application. Now they are calling my app using below URI
kaip.deeplinkSandbox://payment?token=1p51ktwy2qK5sDwNBJy2kP11vK
So how to mention this on my app's Manifests file to open my app, when user trying this above from their app.
i want to seperate schema , host and path from the above deeplink data. Please help to finish this. Thanks!
Add this in your manifest file inside the tag. Activity should be your Launcher activity
<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="kaip.deeplinkSandbox" />
</intent-filter>
And in your Activity,you can get the link like this:-
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical()) {
String uri = this.getIntent().getDataString();
Log.i("MyApp", "Deep link clicked " + uri);
}
If any query, you can ask!!

How to open specific app's specific activity clicking on an external link?

I am developing an android app, in which user must need to login first to use it's features. So as usual user must need to login using provided user credential.
But I don't want user always enter username and password again and again, so that I am just sharing a URL with user and if user click on shared link from anywhere in phone (via mail, or WhatsApp, or Facebook, etc.) phone must prompt to choose my app and app should automatically extract the URL and directly redirect the app to my Home Screen by hitting the login api if username and password are correct.
The shared url will be look like this - http://www.myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx
For now I have managed to prompt the dialog using this code in my AndroidManifest file -
<activity
android:name="com.my.par.activities.UserActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter android:autoVerify="true" tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.myurl.com"
android:path="/sso"
android:scheme="http" />
<data
android:host="www.yoururl.com"
android:path="/sso1"
android:scheme="https" />
</intent-filter>
</activity>
I am getting this clicked URL link in my UserAcitivity's on getIntent() method, like this -
Uri uri = getIntent().getData();
String path = uri.getPath();
So my real question is how to extract the username and password separately and so that I can call my login api by sending this username and password from this url (for ex. http://www.myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx). - Done
And how can I put a dynamic URL or host name, path and scheme in AndroidMenifest file? - Done
How can we force our app to always directly open the app,(don't need to show the chooser dialog) because no use of it even user click on browser nothing gonna happen.
Happy coding :-)
Karzel is also right but it's written in Kotlin, I believe you are expecting the solution for Java I'll answer the same in Java
Uri uri = getIntent().getData();
String strUsername = "", strPassword = "";
if (uri != null) {
strUsername = uri.getQueryParameter("username");
strPassword = uri.getQueryParameter("password");
}
else {
// Your app will pop up even if http://www.myurl.com/sso is clicked, so better to handle null uri
}
To answer your next part about dynamic URL, you can have multiple intent-filters for that purpose
<activity
android:name="com.my.par.activities.UserActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter android:autoVerify="true" tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.myurl.com"
android:path="/sso"
android:scheme="http" />
</intent-filter>
<intent-filter android:autoVerify="true" tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.myurl.com"
android:path="/sso"
android:scheme="https" />
</intent-filter>
<intent-filter android:autoVerify="true" tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.myurl.com"
android:path="/debug"
android:scheme="http" />
</intent-filter>
</activity>
and so on.
To open your app directly by clicking the link and not show a chooser dialog you'll have to use a custom URI, for example:
syn3sthete://myurl.com/sso?username=Scb56745&password=!l0Aeu0yQazxssx
You can change syn3sthete to any custom value you want.
Don't forget to add an intent-filter for your custom URI.
<intent-filter android:autoVerify="true" tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="myurl.com"
android:path="/sso"
android:scheme="syn3sthete" />
</intent-filter>
Try using code like this :
fun getUsernameFromUrl(url: String): String? {
return Uri.parse(url).getQueryParameter("username")
}
fun getPasswordFromUrl(url: String): String? {
return Uri.parse(url).getQueryParameter("password")
}
And call it
val username = getUsernameFromUrl(path)
etc.
UPDATE:
The only "dynamic" way of adding URL I know is using something like this :
https://developer.android.com/studio/build/manifest-build-variables
you can change this URL depending on your Build Variant.
As is said here :
https://stackoverflow.com/a/43339476/8713068
you cannot update Android Manifest directly from code

Deeplinking in Android app

I am trying to enable deeplinking in my app. Here is the code I am running in AndroidManifest to achieve it:
<activity
android:name=".ui.WalletActivity"
android:label="#string/title_activity_wallet">
<intent-filter android:label="#string/title_activity_link_wallet" >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="myapp"
android:host="wallet"
/>
</intent-filter>
</activity>
Is there anything else I need to do in order to make tapping on myapp://wallet to open WalletActivity of my app?
In my app, this works fine:
<activity
android:name=".ui.WalletActivity"
android:label="#string/title_activity_wallet">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<intent-filter>
<data android:scheme="myapp" android:host="wallet" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
I also have extra parameters (myapp://wallet?id=42), which I parse like this:
Intent intent = getIntent();
String providerUrl = intent.getData().toString();
UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();
sanitizer.setAllowUnregisteredParamaters(true);
sanitizer.parseUrl(providerUrl);
String id = sanitizer.getValue("id");
(You can read about UrlQuerySanitizer here)
To test, I'd suggest to send the myapp://wallet link to yourself via email, for example :-) (or adb, as mentioned in the comments)
To test and make sure that your intent is working, you can use adb command as suggested in the deep linking docs
To pass parameters in your uri, simply add them as a normal query parameter (i.e. example://gizmos/path?key=value, then retrieve them by parsing the intent data like this:
Uri uri = getIntent().getData();
String value = uri.getQueryParameter("key");
Make sure you check for nulls etc.

Launch Activity From URL

I am trying to have my application launch when the user browses to a certain url. I have found a few examples and they all have the same things in the manifests but it's not working for me. I have put the intent-filter under an Activity as well as a Receiver.
Here is my manifest snippet:
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:host="www.urbandictionary.com" android:scheme="http"></data>
</intent-filter>
When under the Activity, I tried using onNewIntent and when it was under a Receiver, I tried using onReceiveIntent, both with a simple Log.i call to see if it fired or not. I am not having much luck.
I use this in my manifest.xml file:
<activity android:name=".SomeName">
<intent-filter>
<category android:name="android.intent.category.ALTERNATIVE" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="google.com" android:scheme="http" />
</intent-filter>
</activity>
This will start activity SomeName. I don't use www in the android:host part maybe that will make a difference.
When the activity starts you can get the data that's behind the .com using (for example):
Uri data = getIntent().getData();
if(data != null && data.getPathSegments().size() >= 2){
List<String> params = data.getPathSegments();
String somestuff = params.get(0);
}
Edit: If you wan't to be able to check the host from within the activity, use this method:
data.getHost();

Categories

Resources