Hi i'm setting up my first webview app in android and i try to make my javascript code calls the android code according to this tutorial :
http://android.programmerguru.com/binding-javascript-and-android-code-example/
What i'm trying exactly to do is to play a sound on an onclick event like in my website but it's pretty slow and i read many post on it and i understood why i should use another way to do that.
As i found the tutorial i put in the previous link, i tried to make it work in this way but nothing happened. I create "raw" folder and put in it "click.mp3" file.
I'm probably forgetting something but i don't find it...
MyActivity.java files:
package com.example.mywebviewapp;
import android.media.MediaPlayer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MyActivity extends Activity {
private WebView mWebView;
MediaPlayer mysound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Inject WebAppInterface methods into Web page by having Interface name 'Android'
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
mWebView.loadUrl("http://mywebsite.com");
}
public class WebAppInterface{
public void soundAndroClick(){
mysound = MediaPlayer.create(MyActivity.this, R.raw.click);
mysound.start();
}
}
}
and i call the method soundAndroClick() in my main.js file like that:
function soundAndroid(){
Android.soundAndroClick();
}
Related
package com.example.video_app;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button clk;
VideoView Videov;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clk=(Button)findViewById(R.id.button);
Videov=(VideoView)findViewById(R.id.videoView);
}
public void videoPlay(View v){
String videoPath="android.resource:// com.example.video_app/"+R.raw.sv;//video path
Uri uri= Uri.parse(videoPath);
Videov.setVideoURI(uri);
Videov.start();//start
}
}
I am new to android studio trying to build a simple app of video playing but the video is not playing cant get where the problem is.The video is not playing on emulator nor on my phone. i have tried all the codes mentioned in stackoverflow but could not find any solution... help to resolve this problem
Check out OnPreparedListener. Call start() from inside that event, since then the file will be ready to be played.
You haven't called videoPlay() method from any of the method,
Call videoPlay() method from onCreate() after findViewById() of video view.
and remove View v from videoPlay() method,i think it's useless.
I have an app with the following code on the MainActivity.java:
package com.MyApp;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
import android.view.Window;
import android.view.WindowManager;
import android.os.Bundle;
import com.MyApp.CustomLockscreenPackage;
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "MyApp";
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
}
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
The lines who make the app work on the lockscreen are:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
}
But, the entire app works on the lockscreen, and i need to make only certain screens works on the lockscreen (It's a call app).
I tried creating a native module but don't worked.
i know its possible remove the flags with:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
So, if i can call these lines on my react native screen, it probably can work
[EDIT]: I made a module who does that for me: react-native-keep-awake
[EDIT]: How to use my module:
first of all, install him:
npm install --save https://github.com/willnaoosmit/react-native-keep-visible
(or with yarn, if you want to)
then:
import KeepAwake from 'react-native-keep-awake'; #import the module, of course
KeepAwake.activate(); #when you want to use the app on the lockscreen
KeepAwake.deactivate(); #when you dont want to use the app on the lockscreen anymore
I am using the free map service: 'http://www.opentouchmap.org/' to view various buildings in an establishment using a WebView object.
My question is, is there a GET-parameter or script I can use to create a border so that the user cannot E.g."Go to China and back"?
Here is my code so far:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MapActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
WebView webview = new WebView(this);
setContentView(webview);
webview.setWebViewClient(new WebViewClient());
//No need to worry about security
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.opentouchmap.org/? lat=latitude&lon=longitude&zoom=15");
//TODO: what to do here??
webview.evaluateJavascript("",null);
}
}
I am using a webview to load a web application. Within the page I load I use the HTML 5 geolocation API to detect the users location. This works in all browsers and currently have it working from an iOS application. For some reason I can't get it to work from within an Android webview. When I run the code below I am seeing this message in the stack trace:
[INFO:CONSOLE(24)] "Uncaught ReferenceError: PageMethods is not defined", source: [THE URL FOR THE SCRIPT] (24)
Again this functions just fine in a browser.
package bar.krowd.krowdapp;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView KrowdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
KrowdView = (WebView) findViewById((R.id.activity_main_webview));
WebSettings ws = KrowdView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
KrowdView.setWebViewClient(new WebViewClient());
KrowdView.setWebChromeClient(new GeoWebChromeClient());
KrowdView.setBackgroundColor(Color.BLACK);
KrowdView.loadUrl([I AM PUTTING THE URL HERE]);
}
#Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (KrowdView.canGoBack()) {
KrowdView.goBack();
}
else {
super.onBackPressed();
}
}
public class GeoWebChromeClient extends WebChromeClient {
#Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, true, false);
}
}
}
I have the following listed in my AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
It seems a bug, i solved it downgrading targetSdkVersion from 23 to 21 in app build.gradle
Add this:
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
I am beginner in Android development and want to create Android app with JavaScript.
I can run my page in WebView.
I can run JavaScript in WebView.
I can call Android Toast message (or Dailog alert ) from JavaScript to Android.
But I can NOT run JavaScript function from Android !
Can you help me to fix my code
[ Download an android studio project ]
http://r7eq.weebly.com
Code of MainActivity.java
package com.example.adnroid_script;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
public class MainActivity extends Activity {
public static WebView myWebView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView=(WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new jsInterface(), "FromJS");
myWebView.loadUrl("file:///android_asset/index.html");
}
public class jsInterface{
#JavascriptInterface
public void ToAndroid(String jsMessage){
Toast toast = Toast.makeText(getApplicationContext(),"I sent javascript code to myWebView", Toast.LENGTH_LONG);
toast.show();
WebView myWebView = (WebView) findViewById(R.id.webview);
// ### The bug is here ### //
myWebView.loadUrl("javascript:document.write('<div>Android say : It is working now !</div>');");
}
}
}