I'm working on an Android app that communicates with a web app and I need help figuring out how to stop a process in the Android app from the web app. I need to allow the user of the web app to simply click a button which will stop a process within the android app. Any ideas on how to do this? Any help would be great. Thanks!
Edit: I'm using JavaScript for my web app.
Actually you can call native Android code from WebView. This means that you can call whatever function you want of your Adnroid app from within your Web app. See this:
Calling native code from within web view:
When creating the web view add javascript interface (basically java class whose methods will be exposed to be called via javascript in the web view).
JavaScriptInterface jsInterface = new JavaScriptInterface(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "JSInterface");
The definition of the javascript interface class itself (this is examplary class I took from another answer of mine and opens video in native intent)
public class JavaScriptInterface {
private Activity activity;
public JavaScriptInterface(Activity activiy) {
this.activity = activiy;
}
public void startVideo(String videoAddress){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
activity.startActivity(intent);
}
}
Now if yo want to call this code form the html of the page you provide the following method:
<script>
function playVideo(video){
window.JSInterface.startVideo(video);
}
</script>
So you need to add the appropriate method to JSInterface and call the code from within the Web App.
Related
I have an android app where in some places it may have the URLs (e.g. from push notifications, in some views where text is pulled from BE, etc.). My question is: is there any fast and good way to detect that app opens up a URL and redirect it to the web browser? I want to check the opened URL and in some cases stop app from opening web browser and do my custom action instead. Thanks
Not directly. If you are using Intent Action view and passing like this
ContextCompat.startActivity(
context,
Intent(Intent.ACTION_VIEW, Uri.parse(appLink)),
null
)
You could use a broadcast manager sent it there and from on onReceive method you can call the code to launch web browser from broadcast.
You could perhaps use a WebView and intercept all request calls using this code
mWebView?.setWebViewClient(object : WebViewClient() {
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
LogUtils.d(request?.url)
return super.shouldInterceptRequest(view, request)
}
})
Sorry for the vagueness of my questions here, but I wanted to know if it was possible to login into this website https://oyster.tfl.gov.uk/oyster/entry.do through an a native phone app?
I've seen many apps which access data for the Oyster Card but there is no API for them to access this data and can only think they've logged into the website using the users details then extracting the necessary text to display within their app.
Here is a sample app alongside what the actual website looks like once logged in;
Sure it is, here is an example using WebView:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://oyster.tfl.gov.uk/oyster/entry.do");
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementsByName('j_username')[0].value = '"+ausername+"'");
view.loadUrl("javascript:document.getElementsByName('j_password')[0].value = '"+apassword+"'");
view.loadUrl("javascript:document.forms[0].submit()");
}
});
This will show the common desktop version of the website but if you want a dedicated app for the site, it would be preferable with some sort of API for the site. Without an API, you are left with either the common desktop website, or DOM manipulation of the received html using regular HTTP requests.
I'm now developing an android application with the NFC concept . In that application I need to pick my application if I swipe the NFC card and if I select my application my application must start calling webservice.
when it comes to my problem, If suppose my app crashed ,when I swipe the card next time ,the option to choose the application doesn't open .Instead,my application directly launched and couldn't call the webservice data.
On the whole.I'm getting last page when it crashed .But I need to open as fresh
I came to know that I need to make changes in OnResume() and OnNewIntent().
I used ,
In OnResume()
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mNdefExchangeFilters, null);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
NdefMessage[] msgs = getNdefMessages(getIntent());
byte[] payload = msgs[0].getRecords()[0].getPayload();
//Toast.makeText(this, new String(payload), Toast.LENGTH_LONG).show();
Student=new String(payload);
if(Student.equals(rakesh)
{
new Webservice.execute(""); // Calling SOAP Webservice
}
But,I can't find any changes with my problem .and one more thing that the problem will be resolve after I just open and close an another NFC project
Please help.
Yeah ! I got the solution .I forget a simple thing that I left calling onStop() method and my problem was,when my application stops(when Crashed).It runs in background as the previous activity.
I just tried the following,
public void onStop()
{
super.onStop();
this.finish();
}
This may be helpful to others.
Thanks..
I am working on a phonegap application, with some native pieces.
So far I have notifications running natively, to make sure javascript and phonegap is not required at all. The problem I am having however, is when the notification is clicked I'd like to open the app, or otherwise just bring the app to the front.
How do I do this? I tried setting the main activity in the intent, but it restarts my application every time.
Intent notificationIntent = new Intent(context, main.app.class);
notificationIntent.putExtra(NOTIF_RESPOND, runThis);
notificationIntent.setAction(Intent.ACTION_VIEW);
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Actually in Android there is almost no such thing as "restarting application". Basically the Application - is a bundle of all your services/activities/providers etc. It's either running or not.
If user starts a new Activity - system will check, if application is running - it will create this activity inside the application. If no - will create new Application instance.
So I think you mean that if you set intent for your Main activity - another instance of Activity is launching, am I right?
If so - take a look at the following flag for activity, you should try to set in in the Manifest(or whatever in PhoneGap, sorry). http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
<activity android:name=".YourActivity"
android:launchMode="singleTask"
...
This flag will tell the system not to create a new Activity each type it will be needed, but to reuse already-existing one. Be careful, you should properly implement onNewIntent method in this case to handle this "relaunches", and I'm not 100% sure that it's implemented in PhoneGap.
Good luck
i need an Activity in my Widget Provider. Is there any way to realize that?
The problem is: I want to write a little Facebook Widget but: The Facebook API needs an Activity in the Authorize Function... without this function i get an Facebook Error. Xan i call a Object in my Activity from this Widget ? Or, can i "share" this Object global?
Facebook facebook = new Facebook("X X X");
facebook.authorize(this, new String[] {}, new DialogListener() { });
https://developers.facebook.com/docs/reference/androidsdk/authentication/
Facebook api requires an activity to display a dialog with authentication, you should create an Activity in your app and start it to display auth dialog.