Android DOM controls

I am trying to take control of the Play / Pause html DOM elements (in a browser open on a PC) from an Android device.

There is a <video> tag on the html page (in Google Chrome browser), so I can manage it like this:

 //js code document.querySelector("video").play(); document.querySelector("video").pause(); 

But I want this to run from an Android device, so I use GCM .

I read here and got some insights, but I still have some questions.

  • Firstly, since I write in eclipse and does not see the document variable, it causes an error. So how does eclipse recognize that element in the html page so that I can compile and install apk on the device?

  • Where can I specify the URL of the page I want to chat with? (send play / pause commands)

  • To run js inside java , I use Rhino . I looked through the examples in the documentation , but I'm still not sure if the @JSFunction annotations @JSFunction enough to declare js .

Here is my code:

 import com.alaa.chromote.util.SystemUiHider; import com.google.android.gcm.GCMRegistrar; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import android.view.View.OnClickListener; import org.mozilla.javascript.*; import org.mozilla.javascript.annotations.*; public class MainApplication extends Activity { private final static String GCM_SENDER_ID = "484514826047"; private static final String LOG_TAG = "GetAClue::GCMIntentService"; private Button playButton; private Button pauseButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_application); playButton = (Button) findViewById(R.id.PlayButton); pauseButton = (Button) findViewById(R.id.PauseButton); playButton.setVisibility(View.INVISIBLE); pauseButton.setVisibility(View.VISIBLE); //connect to gcm GCMRegistrar.checkDevice( this ); GCMRegistrar.checkManifest( this ); final String regId = GCMRegistrar.getRegistrationId( this ); if( regId.equals( "" ) ) { GCMRegistrar.register( this, GCM_SENDER_ID ); } else { Log.v( LOG_TAG, "Already registered" ); } Context.enter(); //start Rhino setupListeners(); } @JSFunction public void play() { document.querySelector("video").play(); } @JSFunction public void pause() { document.querySelector("video").pause(); } private void setupListeners() { playButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { play(); } }); pauseButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { pause(); } }); } @Override protected void onStop() { Context.exit(); //stop Rhino super.onStop(); } } 

How do I proceed?

+6
source share
1 answer
  • Firstly, since I am writing in eclipse and do not see any document variable, this leads to an error. So how does eclipse recognize this element in the html page so that I can compile and install apk on the device?

    answering: On your Android device, you simply send a message to the Chrome browser. A.K. action variable that is set to play or pause. After that, the chrome application will pick up the message and act accordingly. You can also send the URL as a variable in the message if you want to be able to play different URLs.


  • Where can I specify the URL of the page I want to chat with? (send play / pause commands)?

    answering: Have you already created the chrome app you want and tested it? He must check with the Google cloud server for messages. This server keeps track of the url for you.


  • To run js inside java, I use Rhino. I looked through the examples in the documentation, but I'm still not sure if the @JSFunction annotations are enough to declare a js function.?

    answering: It seems you misunderstand what the Android application does (sending the game action) and what the Chrome browser does (actually plays the movie).


I hope my answer helped a bit, feedback is welcome :)

+2
source

Source: https://habr.com/ru/post/985807/


All Articles