Can anybody please guide me regarding how to launch my android application from the android browser? Thanks in advance.
Use an <intent-filter>
with a <data>
element. For example, to handle all links to twitter.com, you'd put this inside your <activity>
in your AndroidManifest.xml
:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application.
Of course, if you want to provide tight integration between your website and your app, you can define your own scheme:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, in your web app you can put links like:
<a href="my.special.scheme://other/parameters/here">
And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme://
type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.
Edit: To answer your question, you can use getIntent().getData()
which returns a Uri
object. You can then use Uri.*
methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234
:
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
You can do the above anywhere in your Activity
, but you're probably going to want to do it in onCreate()
. You can also use params.size()
to get the number of path segments in the Uri
. Look to javadoc or the android developer website for other Uri
methods you can use to extract specific parts.
Hi. I did as specified here. When i click on the link in the browser, it shows me the message that "Web page not available. The web page at "my.sample.scheme://" might be temporarily down or it may have moved permanently to a new address".
My app with the scheme is already installed. Is there any kind of permission needed or anything else to launch my app from the browser? Please let me know. Thanks in advance.
Hey I got the solution. I did not set the category as "Default". Also I was using the Main activity for the intent Data. Now i am using a different activity for the intent data. Thanks for the help. :)
Hi, i tried the above code and it diddnt work for me. i am trying to construct the appropriate callback url for linkedIn's OAuth authentication process and for me, after i grant access to my android app to use linkedIn, it never closes the android browser and return back to my activity so i can extract the access token.
here is my code below:
public static final String CALLBACK_URL = "linkedin.android.app://";
private Context mContext;
private LinkedInOAuthService mOauthService;
private static final String TAG = "AuthenticateHelper";
public AuthenticateHelper(Context context) {
mContext = context;
}
public void authenticateApp(){
Log.d(TAG, "authenticateApp");
displayAuthorizePage(getRequestToken().getAuthorizationUrl());
Log.d(TAG, "finish authenticateApp");
}
private LinkedInRequestToken getRequestToken() {
Log.d(TAG, "API_KEY = " + API_KEY);
Log.d(TAG, "SECRET_KEY = " + SECRET_KEY);
mOauthService = LinkedInOAuthServiceFactory.getInstance()
.createLinkedInOAuthService(API_KEY, SECRET_KEY);
Log.d(TAG, "mOauthService = LinkedInOAuthServiceFactory.getInstance()");
LinkedInRequestToken requestToken = mOauthService
.getOAuthRequestToken(CALLBACK_URL);
Log.d(TAG, "LinkedInRequestToken requestToken = mOauthService");
return requestToken;
}
private void displayAuthorizePage(String url){
Log.d(TAG, "url = " + url);
Intent intent = new Intent(Intent.ACTION_VIEW );
Log.d(TAG, "Intent intent = new Intent(Intent.ACTION_VIEW );");
intent.setData(Uri.parse(url));
Log.d(TAG, "intent.setData(Uri.parse(url));");
mContext.startActivity(intent);
}
Manifest file below:
<activity android:name=".MainScreen" 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>
<data android:scheme="linkedin.android.app" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
</application>
You need to add a pseudo-hostname to the CALLBACK_URL 'app://' doesn't make sense as a URL and cannot be parsed.
There should also be <category android:name="android.intent.category.BROWSABLE"/>
added to the intent filter to make the activity recognized properly from the link.
Please see my comment here: http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app/3472228
We strongly discourage people from using their own schemes, unless they are defining a new world-wide internet scheme.
@felix: i had a similar question, then found your answer it is perfect.
I have one downside, if i open the app without clicking a link ... nullpointerexception ... so i tried to make a check if the data is null then don't try to fetch data but that doesn't work either. Any idea how to solve this minor problem?