Is there a way to play Twitch Streams inside Android app (Java) - java

I am unable to play Twitch Streams in my Android application.
Twitch is only providing their embed url for playing Streams.
I am using this url: https://player.twitch.tv/?channel=CHANNEL_NAME
I am using a WebView to load this url by
mWebview.loadUrl(url);
But this method has several issues, like the video tends to autoplay but gets stuck initially. I need to press Pause first and then the Play button to start this stream.

Try using this code.
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
mWebview .loadUrl(url);
setContentView(mWebview );
}

Related

How to automatically fill the fields of a form in a webview?

Basically, i want to fill a field on the paypal register form.
Here's my code:
final String paypal_url = "https://www.paypal.com/welcome/signup/#/email_password";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillField();
}
private void fillField() {
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(paypal_url);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:document.getElementById('paypalAccountData_firstName').value = 'test';");
}
});
}
And this is the result: it loads the page and then displays this:
I had already tried almost every solution on this site, without success (the field was not filled).
So, the problem was Paypal's redirection. The code Paul posted works, but only if it is loaded the US version of the site.
Changes:
final String paypal_url = "https://www.paypal.com/us/signup/account";
And
view.loadUrl( "javascript:window.onload= (function(){ document.getElementById('paypalAccountData_firstName').value = 'test';})();");
You need to wait the entire web page loads, including its content (id="paypalAccountData_firstName")
So you should change
view.loadUrl("javascript:document.getElementById('paypalAccountData_firstName').value = 'test';");
to
view.loadUrl( "javascript:window.onload= (function(){ document.getElementById('paypalAccountData_firstName').value = 'test';})();");

How to show custom error message on webview

I'm trying to create custom error message for my app which is using webview. so far i'm using below code which has a very minimal problem... when i click links on the webview the page is reloading the same page instead of processing the clicked link. my links on the webview are download link so either it should download using the same app or open down-loader options
protected void fetch_web(){
final WebView web;
web = (WebView) findViewById(R.id.voice_mail_view_port);
NoNetwork = (Button) findViewById(R.id.btn_no_internet);
NoNetwork.setVisibility(View.GONE);
String mdb = "http://192.168.23.1/default.php";
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
final Activity MyActivity = this;
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Fetching feeds...");
MyActivity.setProgress(progress * 100);
loader();
if(progress == 100) {
MyActivity.setTitle(R.string.app_name);
deLoader();;
}
}
});
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl((mdb));
}
protected void loader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.VISIBLE);
}
protected void deLoader(){
loading = (ProgressBar) findViewById(R.id.loader);
loading.setVisibility(View.INVISIBLE);
}
Sample download link http://192.168.23.1/download.php?id=1
Can someone help me out, i suspect my error is coming from here
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String
failingUrl) {
deLoader();
alert("No Internet Connection","Let's get connected to see feeds");
web.setVisibility(View.GONE);
NoNetwork.setVisibility(View.VISIBLE);
}
});
Try forcing the app to open links in other browsers
Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
If it fails let me know to help more.

Open files with other apps in webview

I'm building a simple app, in which I use a webview in Android Studio, in the app, i search a movie, and then I download the torrent.
When I download in the web, it automatically send me to the utorrent app, but when I download it from my app, I can't open it.
Sorry for my English and for my poor knowledge.
My java code:
public class MainActivity extends AppCompatActivity {
WebView web;
EditText et_peli;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web=(WebView)findViewById(R.id.web);
et_peli=(EditText)findViewById(R.id.et_peli);
}
public void buscar0nclick(View v){
WebSettings conf = web.getSettings();
conf.setJavaScriptEnabled(true);
web.loadUrl("http://www.miltorrents.com/?pTit=" + et_peli.getText().toString());
web.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".torrent")) {
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
request.setDescription("Description for the DownloadManager Bar");
request.setTitle("YourApp");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
else view.loadUrl(url);
return true;
}
});
}
}
Just to make sure I have this right, you want to send your user, once they download the torrent to the torrent's app to view the movie, correct?
I would check to see if that torrent app has a broadcast receiver that you can search for. If so, you could just pass along the information. If not, you have to look for a way to have that media file play within your app, or use one of the default movie players.

How to check if Link is offline or broken with Android Webview?

I use an Android Webview to load a URL like this:
mWebView = (WebView)v.findViewById(R.id.webView);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl("someUrl");
How can I detect if the link the webView should load is offline or broken?
Yes you can do detect broken links in WebView using onRecicedError() in WebViewClient Class.
You have to create an object for WebViewClient and implement onRecicedError() method and set WebViewClient Object using setWebViewClient() method in WebView
Eg:
webView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(errorCode == 404){
Log.d("Webview", "Invalid URL: "+url);
}
else if(errorCode == 500){
Log.d("Webview", "Internal Server error: "+url);
}
}
#TargetApi(android.os.Build.VERSION_CODES.M)
#Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});

Nothing happens when I click a link inside my webview

This is a part of my android app, i have created a webview...but when i click on any link inside the webview...nothing happens....here is my code...i want to launch the link either in any browser or any installed download manager...i m a newbie..please help me out with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
setupActionBar();
// no need to use title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://vesit-d7b.host22.com/Assignments/ECCF.html");
}
Please use this code and check that your url is being caught by the webview or not.
myweb.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
});
see what url is coming when you click the link.....
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.v("here","the link is ::" + url);
view.loadUrl(url);
return true;
}
and then copy it on browser to see whether it is a valid link or not..
#Ritesh remove the shouldOverrideUrlLoading method it will work

Categories