how to open android default share dialog in webview - javascript

i have created an app using webview and now i want to create a share button (in web and js) to open android default share dialog for user.
But this approach does not work:
const sharePromise = navigator.share(data);
Because this is not supported in Android Web View.
what can i do?

you can open share by calling custom JS bridge function. Like below
#JavascriptInterface
fun share(pMessage: String) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_TEXT, pMessage);
sharingIntent.setType("text/plain");
startActivity(Intent.createChooser(sharingIntent, “ChooserTitle"));
}

Related

Is it possible to detect and do an action when user closes an Android/IOS Cordova/Capacitor App?

I'm developing an App with Capacitor and Quasar Framework. I wanted the app to log and close any pending notification from the app when the user closes the app(not background, really closing it by mobile interface like swiping right on "opened apps menu".
I'm using background to track geolocation, so "Terminate" event is not really when the app closes.
I tried using the window.addEventListener("beforeunload") and I developed a plugin in Java that tried to throw an event to a listener: Runtime.getRuntime().addShutdownHook, neither helped me handle that.
Is that something possible? I wanted to log the closing and check if there is a notification still showing to the user, so I can close it.
Capacitor Plugin code
public void load() {
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("appClosed");
JSObject ret = new JSObject();
ret.put("appClosed", true);
notifyListeners("appClosed", ret);
}
});
}

Android WebView push notification?

I need to send a notification (not necessarily a push notification) through an android webview. I saw that the Notification API was not compatible with Android Webview on MDN. The other APIs I saw seemed to be based off of window.notification.
Does anyone know of any API or JS that sends a notification through an android webview?
I saw this post from 6 months ago with essentially no activity except a vague mention of firebase. Would that be helpful?
Thanks for your answers!
I don't have enough "reputation" to post a comment but could that be useful to you?
Android Push Notification with WebView?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
Hopefuly you can make it work from one of those example
Read the documentation entry for Binding JavaScript code to Android code.
This allows you to use javascript to trigger the execution of android code.
First you have to register the Javascript Interface on android, so that you can trigger android code from javascript.
JAVA
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And define a method which does your action if the javascript is called. In this example show a toast.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
↑ You need to change that part to show your push notification instead. ↑
Then you can trigger the android code from javascript like this:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
I did not tried it myself. But I would try to create a javascript method which makes an ajax request to a server in certain intervals and checks if there are new notifications to send and if true then call the android code to show the message.
However, you will have to make sure to only show the notification once somehow... maybe set a cookie containing the notification ID and set it to true, so that the android code is not getting triggered again.
You would need to provide the notifications for example as a .json file in JSON format. You can upload that .json file to your webserver somewhere. Then pass the content to android and parse it accordingly.
One Simple workaround use JSInterafce - communicate webview to native.tutorial
In that JSInterface pass, the desired parameter for notification and then use android system notification API to generate the notification.

Open link in device's default browser. Crosswalk android application

I am creating an app for Android devices using HTML and JavaScript. I am using Crosswalk (15.44.384.12) to bundle this into an Android application, which pretty much creates an android app with a web browser built in, to run my application.
I have everything working on the Android device, but I am struggling to find out how to open a link from my app in the device's default browser using JavaScript.
If I use window.open(), it will just load within my app, which is not what I want.
I have tried using window.open('http://example.com', '_blank'), I have also tried '_system', to no avail.
Same here. All hrefs and window.open calls are opened in the WebView.
We can use a workaround that was also possible in Cordova: to intercept URLs in native Java code.
First create a custom XWalkResourceClient to intercept your urls depending on your needs:
XWalkResourceClient myResourceClient = new XWalkResourceClient(xWalkWebView){
...
#Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
if(url.contains("whatever")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
};
and then in your activity, you can set that client to the XWalk view:
myXWalkWebView.setResourceClient(myResourceClient);

How to navigate to phone setting screen from a webapp

I am developing a web app using sencha touch for all mobile devices. My application uses GPS feature. I would like to know how can I navigate to the native setting screen of the phone from my web app for the user to switch ON the GPS feature.
If the GPS is not ON, it will prompt user a popup which will take the user to location settings screen to turn ON the GPS.
Is this possible ?
Please let know. I have seen this feature in many apps, even in google maps.
It is not cording practice to do that. You can simply state the steps to be followed.
IF you are running webapp on Phonegap or Android web view, then you can do like this.
Read this article how to bind Javascript function to Native app.
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
Android Code
public class WebAppInterface {
/** Show a toast from the web page */
#JavascriptInterface
public void goToSettingPage() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
JavaScript Code
<input type="button" value="Say hello" onClick="openSettingPage()" />
<script type="text/javascript">
function openSettingPage() {
Android.goToSettingPage();
}
</script>

appcelerator - convert windows app to mobile app

I made a great app for windows with appcelerator and I want to convert it to mobile (android).
Problem is, unlike creating an app in windows where the default launcher is "index.html" and I can have all my javascript/css/html mixed in together, the mobile default launcher is app.js.
I've tried the following:
var webview = Titanium.UI.createWebView({
url : 'index.html'
});
var window = Titanium.UI.createWindow();
window.add(webview);
window.open({modal:true});
This works great however none of the api's I'm using inside of index.html are being run, it just alerts an error (undefined).
Does anyone know how to fix this issue?
EDIT:
There are only two API's I'm using in my app:
var db = Titanium.Database.open('app_data');
var device_id = Titanium.Platform.id;
you dont have access to all of the API from the webview, see link below
http://developer.appcelerator.com/question/8991/webview-usage-guideline
you will need to do most of your business logic in the js files and through the event mechanisms move data from you app to the ui level

Categories

Resources