Reputation: 1430
I would like to document that knowledge in public so that others (including myself) can find it later.
Upvotes: 0
Views: 116
Reputation: 913
Using Android standards
You should use a custom link and intent-filter for this purpose:
make a link with href set as
android-app://com.example.android/http/example.com/gizmos?1234
in your app's manifest file declare an intent-filter as
<activity
android:name=".MainActivity"
android:label="MyApp"
android:screenOrientation="portrait" >
<intent-filter>
<data android:scheme="android-app"
android:host="com.example.android" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
in your Main activity handle the incoming intent as :
//required only if you want to get some params from url
Intent intent = getIntent();
Uri data = intent.getData();
This would open MainActivity of the app if its installed on the device or take the user to example.com otherwise.
References:
https://developer.android.com/training/app-indexing/deep-linking.html https://developers.google.com/app-indexing/webmasters/server
Upvotes: 1
Reputation: 1430
If you use your own web page in WebView
then this can be the solution:
MainActivity.java
public class MainActivity extends ActionBarActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadUrl("file:///android_asset/index.html");
}
public class WebAppInterface {
Context mContext;
public WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void openMyApp() {
PackageManager pManager = getPackageManager();
// here you should use a package name of the application
// you want to open
Intent launchIntent = pManager.getLaunchIntentForPackage("com.android.mms");
startActivity(launchIntent);
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webviewtest.MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
assets/index.html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body style="text-align: center;">
<script type="text/javascript">
function openApp() {
Android.openMyApp();
}
</script>
<h1>Open app test</h1>
<button type="button" onclick="openApp()">Click Me!</button>
</body>
Upvotes: 1