Android web view invoking and clicking element - java

I was working on a mobile app, the app plays music from some website using jplayer and changes from one song to another with out no user interaction, my first trail to build the app using phonegap which failed because HTML5 media tags are not allowed to start automatically on mobile devices because of bandwidth issue,the user must click some button to play the media so i am trying some thing new as below,
Create a native android app with a webView
Set the url of the webview url to a file in Assets folder
After the page is loaded and the media is ready to be played, programmatically click on the play button which is in the webview using java so that it can start playing
I have given the play button in the webview an id of playbtn
My java code
I have created a button in order to execute the clicking event of the playbtn.
final WebView wv;
Button playbtn;
wv = (WebView) findViewById(R.id.webView1);
playbtn = (Button) findViewById(R.id.button1);
wv.clearCache(true);
wv.clearHistory();
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv.loadUrl("file:///android_asset/index.html");
playbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "test", Toast.LENGTH_SHORT).show();
// the toast shows up
wv.loadUrl("javascript:(function(){"+
"l=document.getElementById('playbtn');"+
"e=document.createEvent('HTMLEvents');"+
"e.initEvent('click',true,true);"+
"l.dispatchEvent(e);"+
"})()");
//nothing happens here
}
});
I don't thing using JavaScript interface will work, since there is no user interaction, is there any way to accomplish the clicking of the playbtn programmatically?
Update
Once playback has started clicking the button is now resuming the music.

Related

HTML5WebView fullscreen youtube video consumes back button click

In HTML5WebView.java (pastebin link)(source) I have:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("TAG - BACK PRESSED IN WEB VIEW");
return super.onKeyDown(keyCode, event);
}
And in the activity that starts the web view I have:
#Override
public void onBackPressed() {
System.out.println("TAG - BACK PRESSED IN WEB PLAYER ACTIVITY");
super.onBackPressed();
}
Now when I'm playing an embedded youtube video normally (not fullscreen), both methods are called when I press the back button. When I put the video into fullscreen mode (using the youtube player fullscreen button), none of the methods are called. My only guess is, the back button is being consumed by the web view to undo the fullscreen action (but even that doesn't work).
I am trying to get the back button to immediatly kill the web view, even if there is a video in fullscreen mode.
See the problem in action here, had to post it externally because the gif > 2MB
If I was you I would delete the embed play of youtube video's or ask google that won't be seen as abuse. Because the embed play of youtube video's can be seen as a abuse of this:
Your app violates our Device and Network Abuse policy by downloading,
monetizing, or otherwise accessing YouTube videos in violation of the
YouTube Terms of Service or YouTube API Terms of Service.
More Info: https://play.google.com/about/privacy-security/device-network-abuse/ https://www.youtube.com/static?template=terms
For example, your app contains: YouTube background play functionality
This is a violation of the YouTube Terms of Service.
That is what I got in my experience because I also made a app where people could watch some YT embed video's but after some updates the app wouldn't be update becuase I got this error that it is abuse so I am warning you that's all.

How can I prevent a user from pressing the back button, because if the user does it closes the application and restarts it from the first activity

Hey guys I'm currently developing an application for my final project, the concept is a contact keeping app but with user login and registration. When the user enters their credentials and presses the login button it starts an Intent to move to another page, on this Intent I end it with the .finish() method so the user can't go back but when that is executed and lets say by accident the user presses the physical back button on the device the application will close and if you try to open it again by going on the multitask physical button on the device and you select it, it starts the application again from the beginning (the login screen) how can i make it that if the users presses it by accident they can open it again from the multitask or the physical icon of the application so the it picks up on where it left at (the display activity after you login it) essentially not restarting the application.
Is that even possible?
Thanks in advance
Override onBackPressed() in your activity and remove super.onBackPressed().
#Override
public void onBackPressed() {
// super.onBackPressed();
}
First of all to stop device default back key event you have to remove super.onBackPressed() from onBackPressed() of activity
#Override
public void onBackPressed() {
// super.onBackPressed();
}
Now to keep the flow management after login, you have to save the activity name or any other value to SharedPreferences so whenever you come again to the application you will make a check for last activity after login. And navigate to the same.
SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
//add this to onCreate of the activity where you want to come directly
editor = editor.putString("LAST_ACTIVITY", "MULTITASK_ACTIVITY/*Navigated activity name*/").commit();
Now make a check on app start in loading screen:
if(preferences.getString("LAST_ACTIVITY", "").equalsIgnorCase("MULTITASK_ACTIVITY")){
//Navigate to Mutitask activity
}else{
//Navigate to other activity
}

How can I stop webChromeClient from opening files in the Android Browser

I am developing an Android App using WebView which loads a locally stored HTML5/JQuery Mobile app in the assets folder. The App gets content from a REST service on a domain I own. The content is a mixture of HTML content and downloadable files.
My problem lies when a user clicks on a button to download a file, a Browser window launches and the file is downloaded, which is great. However my App then loses focus and I have a blank browser on screen. I have done a bit of Googling and have read a few answers on Stack Overflow and have adapted my code to set a Download Listener, but this still loads the Browser. Please advise. My code is listed below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
webView.loadUrl("file:///android_asset/www/index.html");
}
Have you tried using the Android DownloadManager (please see http://developer.android.com/reference/android/app/DownloadManager.html) instead of starting a VIEW Intent? I think that you'd just need to enqueue a download from your DownloadListener.

Android: How to close application opened from my app when all sent to background

Here is my problem. I am developing app that loads some documents from server. I open document in another app via Intent.ACTION_VIEW. This is all working just fine. Problem is that whole app is pin protected so I have to capture events such "sent to background" or "screen lock" to bring up pin screen afterwards and this is not working when another app is opened above mine. So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. This is security issue that needs to be fixed.
Here are some code snippets:
Opening document:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivityForResult(intent, 1);
capture sent to background:
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
IN_BACKGROUND = true;
Log.i(PinUtil.class.getSimpleName(), "App sent to background ");
} else {
IN_BACKGROUND = false;
}
}
My question is: Is it possible to detect if my app is sent to background when another app is opened? How not to open another app when my launcher icon is pressed.
Thanks for all responses.
Regards
Lubos
In order to fix this problem:
So if user opens document then press home button, click on my launch
icon from menu then he gets again external app with opened document
and with back button access my app again. This is security issue that
needs to be fixed. Here are some code snippets:
You need to make sure that, when you launch an external app for the user to view a document, that the external app does not run in the same task as your application. It needs to run in a new, separate task. You can do this like this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure app runs in separate task
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivity(intent); // Can't use startActivityForResult() here
However, you can't use startActivityForResult() when you launch the external viewer, because an activity running in another task cannot return a result to you. However, most external applications won't return a result when launched with ACTION_VIEW anyway, so it probably isn't a problem.
Then you asked this:
My question is: Is it possible to detect if my app is sent to
background when another app is opened? How not to open another app
when my launcher icon is pressed.
There should be some answers on StackOverflow that can help you determine if your application is in the background (it isn't actually that easy to determine this).
My explanation above should answer your 2nd question. If you don't launch other apps in your task, then only your app will be launched when your launcher icon is pressed.

How can I play videos of a specific youtube channel on my application without the help of browser or youtube application on android

Here I'm using this code,
public class WebTV extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webtv);
mWebView = (WebView) findViewById(R.id.webTV);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.youtube.com/banglanews24");
} }
but it'll play the video on the browser directly without showing the channel page on the webview, but instead of that if i use,
mWebView.loadUrl("http://www.youtube.com/banglanews24");
it'll show the channel page inside the webView but could not load the or render the videos.
I've also tried,
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/banglanews24")));
but none of this work for me.
Can any one give me a solution, through which i can play this youtube channel in side my application, like you tube application for android. Can any one help me out?

Categories