how do i handle this string to open my app - android

i get this string in a browser redirect
intent://view?id=123#Intent;package=com.myapp;scheme=myapp;launchFlags=268435456;end;
how do i work with it ?
found in : http://fokkezb.nl/2013/09/20/url-schemes-for-ios-and-android-2/

You have your answer in the part 1 of the same article :
http://fokkezb.nl/2013/08/26/url-schemes-for-ios-and-android-1/
Your activity must have an intent filter matching the given intent
Here you have :
package=com.myapp;scheme=myapp
Your app package must be com.myapp and the url scheme is myapp://
So you must declare your activity like that :
<activity android:name=".MyActivity" >
<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="myapp" />
</intent-filter>
</activity>
Then your activity will be automatically opened by android.
Optionnaly you can work with the uri received from your code, for example in the onResume method (why onResume ? -> because it is always called after onNewIntent) :
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null && intent.getData() != null) {
Uri uri = intent.getData();
// do whatever you want with the uri given
}
}
If your activity uses onNewIntent, i recommend to use setIntent so that code above is always executed on last intent :
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Does this answers your question ?

Related

How to correctly handle custom URL scheme in my app (avoid handling multiple times)?

I have added a custom URL scheme to my Android 4+ app to be able to deep link to some view/activity:
// AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<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="myapp"/>
<data android:host="webservice"/>
</intent-filter>
</activity>
// MainActivity
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri.getScheme().equalsIgnoreCase("myapp") && uri.getHost().equalsIgnoreCase("webservice")) {
Intent settingsIntent = new Intent(MainActivity.this, WebServiceSettingsActivity.class);
startActivity(settingsIntent);
}
}
}
This works fine, when a link like myapp://webservice/something is used, my app is startet and the WebServiceSettingsActivity is shown.
BUT: When pressing the back button, the WebServiceSettingsActivity is shown again. It seem, that MainActivity is re-startet with the same intent as before and thus the settings are shown again...
This can be repeated indefinitely. How to solve this?
Your MainActivity is opening your WebServiceSettingsActivity, but you're never finishing the former. This leaves you with 2 choices:
Call this.finish(); immediately after calling startActivity(settingsIntent);
This will kill your MainActivity so that there is nothing to return to after finishing the WebServiceSettingsActivity.
2 (better). Open your WebServiceSettingsActivity with startActivityForResult() instead of startActivity(), and finish your MainActivity when, and only when, returning from your WebServiceSettingsActivity:
private static final int REQUEST_CODE_WEB = 1;
public void onCreate(Bundle savedInstance) {
...
if (...) {
startActivityForResult(settingsIntent, REQUEST_CODE_WEB);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_WEB) {
finish(); // Or do whatever you want, such as load your main content
}
}

android firebase dynamic link PendingDynamicLinkData is null direct through app

Problem: Why is "Short dynamic links" created programatically wont open/launch the app directly?
I want to launch app directly when user clicks the dynamic url created dynamically by android app.
When clicking dynamic short link created dynamically by android app the following things happen,
1.Option show two options one is through chrome other is through app
2.if i choose chrome option, browser opens, shows a loading dialog box and launch app with PendingDynamicLinkData data
3.but if i choose app option, app lauches app but PendingDynamicLinkData is lost or null.
Any Help would be great. my manifest setting is below
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="myapp.page.link" android:scheme="http"/>
<data android:host="myapp.page.link" android:scheme="https"/>
</intent-filter>
You should handle your PendingDynamicLinkData in activity to override onCreate and onNewIntent. Like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null) {
handleDeepLink(intent);
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null) {
handleDeepLink(intent);
}
}
private void handleDeepLink(Intent intent) {
FirebaseDynamicLinks.getInstance().getDynamicLink(intent).addOnSuccessListener(pendingDynamicLinkData -> {
if (pendingDynamicLinkData != null) {
Uri deepLink = pendingDynamicLinkData.getLink();
if (deepLink != null) {
// todo .....
}
}
});
}
I don't know the correct solution but I have found a trick that works.
First make sure that you have added the following intent filter to the activity that is supposed to handle the dynamic link:
<intent-filter android:autoVerify="true">
<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="http" />
<data
android:host="example.com"
android:scheme="https" />
</intent-filter>
Source
Then in your activity, use the following code to get the link:
String link=null;
if (getIntent().getData()!=null){
link=getIntent().getData().toString();
}
In the manifest, add the following intent filter to the activity that will handle the url:
override OnStart method of the target Activity:
public override fun onStart() {
super.onStart()
FirebaseDynamicLinks.getInstance()
.getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
#Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
// Get deep link from result (may be null if no link is found)
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
}
// ...
}
})
.addOnFailureListener(this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "getDynamicLink:onFailure", e);
}
});
The complete guide here.
For some reason, i couldn't make it work in the same activity that as action MAIN.

Receiving Simple Data Through Intent for Android App while App is in background

I have an app that receives simple data (text) from other apps through the other app sharing to my app. Part of the manifest looks like this:
<activity
android:name=".presentation.ui.activities.MainActivity"
android:launchMode="singleTop">
<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.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
When the activity hasn't been created yet, I share the text data from the other app to my app and handle the intent with action intent.action.Send in the onCreate method. However, I also want to be able to share the text data while the app is in the background. When the app is in the background and I share the text data from the other app, the action is intent.action.MAIN in the onRestart or onResume methods.
This is the handleIntent method
private void handleIntent(){
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
// logic
}
}
}
How do I receive the text data from the other app while my app is in the background?
I have figured it out.
I overrode the newIntent method
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
Sending Simple Data to Other Apps
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Receiving Simple Data from Other Apps
Update Your Manifest
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
Handle the Incoming Content
void onCreate (Bundle savedInstanceState) {
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}

How to push an ingested deep link back to a web browser on Android

I'm currently having trouble where I am ingesting a deep link in my application, but after further review of the URL, I want to push the URL back to the web browser to display since my application does not provide the capability of using it. When I try to open the URL in a web browser, nothing happens since I have it defined to be a deep link. I tried sanitizing in the manifest with android:pathPrefix="/#!/foo", but the # character is not supported and causes the whole thing to not work.
AndroidManifest.xml
<activity
android:name=".activities.IntentForwardingActivity"
android:launchMode="singleTask"
>
<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="www.example.com"
android:scheme="https"
/>
</intent-filter>
</activity>
IntentForwardingActivity.java
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_VIEW)) {
Uri data = intent.getData();
handleDeepLinking(data.toString());
}
}
private void handleDeepLinking(String url) {
Intent intent;
if (url.contains("/#!/foo")) {
intent = HomeActivity.newIntentForFoo(this, url);
} else {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
}
startActivity(intent);
finish();
}

How to launch an app using a deeplink in android

I want to launch app using my own app but not by giving the package name, I want to open a custom URL.
I do this to start an application.
Intent intent = getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
startActivity(intent);
Instead of package name is it possible to give a deep-link for example:
"mobiledeeplinkingprojectdemo://product/123"
Reference
You need to define a activity that will subscribe to required intent filters:
<activity
android:name="DeepLinkListener"
android:exported="true" >
<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="host"
android:pathPattern="some regex"
android:scheme="scheme" />
</intent-filter>
</activity>
Then in onCreate of your DeepLinkListener activity you can access the host, scheme etc.:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent deepLinkingIntent= getIntent();
deepLinkingIntent.getScheme();
deepLinkingIntent.getData().getPath();
}
Perform check on path and again fire a Intent to take the user to corresponding activity. Refer data documentation for more help.
Now fire a Intent:
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData(Uri.parse(DEEP_LINK_URL));
Don't forget to handle the exception. If there is no activity that can handle the deep link, startActivity will return an exception.
try {
context.startActivity(
Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(deepLink)
}
)
} catch (exception: Exception) {
Toast.makeText(context, exception.localizedMessage, Toast.LENGTH_LONG).show()
}

Categories

Resources