How to get Android deeplink data - android

After setting up an app for deep linking. by creating an activity to launch when the link is opened in the browser. How do I go about getting the link that the browser used to start the intent?
Let us say the app catches the URL: "https://example.com/foo/bar?param=1&data=extra".
In the Activity launched I want to use the URL which opened the app. I am thinking maybe something like int the activity perform
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
I dont know the variable to access the lauch URL from Uri data

It turns out I was on the right track all I do is
data.getQuery();
To get the last part of "https://example.com/foo/bar?param=1&data=extra" ie "param=1&data=extra"
Otherwise, call another function of data like.function() to get other aspects of the URI eg the whole URL that led to the opening of the activity
Stackoverflow makes you think half the time I always find the answer Immediately after I post a question.

Related

Android / Xamarin, Open file with intent and close app after back button pressed

I have a problem with a feature of my application.
I need to open external files to display them like .jpeg or .xls for example. For that I use Intents to open them with the default system application and it works fine.
But in a case where we will first open a document, for example a photo. this will display in the default app and then I go back and forth in my app. Everything is OK
But then I'm going to open another standard .xls document, it'll open the document in the appropriate application and going back will show the opened image first and not my application again.
I don't understand why it happens like this.
What I would like is that when we do previous in a file viewer app, the app quits and doesn't stay in the ActivityManager.
Is it possible ?
I have already tried some intent flags but without success.
My code here :
Android.Net.Uri fileUri = FileProvider.GetUriForFile(Android.App.Application.Context, "com.app.fileprovider"
, file);
Intent intent = new Intent(Intent.ActionView);
intent.SetData(fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.NoHistory);
Android.App.Application.Context.StartActivity(intent);
return true;
Thanks a lot for the help
Max B
SO !
My bad, It was not a real problem.
It was a design error.
Indeed during my call of my function to call the intention I linked my opening event without then unbinding it. So that with each call I recalled X times my file opening function without ever destroying my variable or my variable instance.
The Intent Works Well
Thanks for the comments

ANDROID: open an URL in external browser, while continuing the application

In the starting activity of my app I show a dialog to user and ask if he wants to see some contents in my website or not.
If he clicks No, the dialog disappears and I call continueActivity() to do some process and go from current activity to MainActivity.
If he click yes, I want to open the webpage in the external browser and again I call continueActivity() to do some process and go from current activity to MainActivity.
The problem is in positive state. This is my code in positive state:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaa.ir"));
startActivity(browserIntent);
continueActivity();
Because of calling continueActivity(), the external browser can't open and if I don't call continueActivity(), the URL opens in the external browser and the app sticks in the current activity.
So how could I open the URL and at the same time, continue the process and go to other activities.
I usually use
startActivityForResult(browserIntent, BROWSER_REQUEST);
and override onActivityResult(int reqCode, int resultStat, Intent intent)
if(reqCode == BROWSER_REQUEST) {
continueActivity();
}
Once you call startActivity, your current Activity will follow through the exit of the Activity lifecycle. This is unavoidable.
If you have processing you want to continue in the meantime, you should consider a Service. If you want the App to return to its current state, you need to store relevant data and load your Activity to its previous state (or next intended state).
in both ways ContinueActivity is opening. i don't know you architecture. You could just add to intent some extra like
intent.putBoolean("openExternalLink", dialogResultHere);
Then in continueActivity you will got this intent like
getIntent().getBoolean("openExternalLink")
also dont forget to delete this option, otherwise you will open browser each time after Activity recreation (screen rotation, minimize, etc.)
getIntent().removeBoolean("openExternalLink");
P.S. signatures of methods could be littlebit different, but general idea is here

Getting data from intent

I am little confused about getting data from intent of an activity, here is what I am trying to do.
I am opening my first activity
I press home button and my activity(application) goes in background
I go to download folder and select one doc file it gives me Open with feature with my application in list.
I select my application.
Now instead of getting resume it calls create method of activity(I think its ok because second instance of my app(activity is created))
Here I try to get data from intent using intent = activity.getIntent();
But I dont get anything(getting null expecting path of this doc)
If I first open my app with Open with feature(No background activity right now) I get file path successfully but when I pres home button and my app goes background again and if I press my app from application list again intent has same data(expecting null in this case.)
What I am trying to achieve:-
I am simply trying that if user is coming from application list intent data should be null and if he is coming from Open with function then he intent data should be path of file.
One more thing I am trying this behaviour in Appcelerator Titanium but as this is native behaviour(handling life cycle of activity and I can do this in titanium) but having no luck.
Thanks.
I some what manage this by giving my activity as 'singleInstance' in manfiest file. So now I can be sure that at a time same instance is running.
Now for handling intent data I am registering onNewInstance method. By using this intent data is get affected everytime when I comes from background or when when I select any file to open with my application.
Thanks.

ACTION_SEND intent android

I am trying to pass a url to a specific app using the ACTION_SEND intent, I want to by pass the chooser and just go straight to the app i desire but it doesn't seem to take the url unless i use the chooser..
private void shareIt(){
Intent pC = new Intent(Intent.ACTION_SEND);
pC.setType("text/plain");
pC.putExtra(Intent.EXTRA_TEXT, "http://www.bob.com");
pC.setType("text/plain");
pC.setClassName("com.sec.print.mobileprint","com.sec.print.mobileprint.UI.WebPrint");
//startActivity(pC);
startActivity(Intent.createChooser(pC,"Share jon"));
}
if i comment out the last line and comment back in the line before it.. it opens the app i want bypassing the chooser, but the app opens to google instead of bob.com.. if i leave it as is.. it brings up the chooser and should i choose the app it goes to bob.com .. how can i get it to go to bob.com while bypassing the chooser?
I suspect that the Intent.setClassName method you’re calling takes an unqualified class name as its second argument (after all, why bother repeating the package name qualification?). Alternatively, you can use setClass instead.
Are you sure you need to pass the URL via EXTRA_TEXT and not by pC.setData(Uri.parse("http://www.bob.com");?

Opening a web browser from an Android application

I created an app to simply open a web browser and go to a specific website. However, after it's run for the first time, if I go to another app and run the app again, I will get nothing except the black screen. I knew I can find the page from web browser, however I wonder if there is a way to bring the content back when I launch the app and the user could get back to work from where he/she stopped.
here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
Thanks guys
Take a look at the activity lifecycle. You may want to move your intent to OnStart() for when the application comes back to the foreground after being stopped.
The activity may still exist when you run your app a second time; perhaps onCreate is not called.
After you throw the Intent to run the browser, shut down the activity so that onCreate happens every time.
If your application is here only to give a link to a website, this is much overhead in my opinion. Android users can make a shortcut to any website they want on the desktop, the shortcut icon being shown with the site's favicon in its corner.
Try this if you want to open the browser OUTSIDE your app. I know it seems like a simple(and trivial) edit, but I've never had issues setting it up this way.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Uri uri = Uri.parse(url);
Intent i= new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
}
Why don't you use the android.webkit.WebView which will allow you to directly browse web pages in your app. Here the web content will remain persistent as long as the application is running. On the chance that your application stops you could save the url using WebView.getURL() which would allow you to continue from where you left of the next time you start your app. If this is what you want to do let me know and I will post some sample code.

Categories

Resources