I have tried many times to upload my app, that has a json from Wordpress and webview to open a link from json, I made the changes that was suggested here. But, when I try to upload, Google sends me this message:
Modify your app to make sure it doesn’t access or use a service or API in a manner that violates its terms of service; for example, by enabling background play of YouTube videos.
This is my webview code:
public class Webview extends AppCompatActivity {
WebView myWebView;
private boolean mShouldPause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String link = getIntent().getStringExtra("link");
myWebView = (WebView) findViewById(R.id.vebview);
myWebView.loadUrl(link);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
#Override
public void onLoadResource(WebView webview, String url) {
super.onLoadResource(webview, url);
if (url.contains("youtube.com")) mShouldPause = true;
}
});
//button for share the link of webview
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String shareBody = "you body here";
String shareSub= "suject here";
intent.putExtra(Intent.EXTRA_SUBJECT,shareSub);
intent.putExtra(Intent.EXTRA_TEXT,shareBody);
startActivity(Intent.createChooser(intent,"Compartilhe"));
}
});
}
#Override
public void onPause() {
super.onPause();
if(mShouldPause){
myWebView.onPause();
}
mShouldPause = false;
}
Somebody knows what I'm doing wrong?
(Posted on behalf of the OP).
I found the solution, and was in my app, I miss the permission on manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Related
I am trying to mirror a website in an android app which I already got a way around it, but am having issues with sharing information inside the app to social media, like when it is clicked in the app to share information to WhatsApp, Facebook, etc I want the app to be able to find each app and open the app with the information, I've tried all way but it's not working kindly help if you can
Thanks in advance
my code
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://eliteglobalnetwork.net/");
webView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
}else {
super.onBackPressed();
}
}
}
public static final String WEBSITE_ADDRESS = "website_address";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra(WEBSITE_ADDRESS);
if (url == null || url.isEmpty()) finish();
setContentView(R.layout.activity_webview); //name of your layout
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
}
If want to open in same app -
Intent i = new Intent(YourActivity.this, WebViewActivity.class);
i.putExtra(WebViewActivity.WEBSITE_ADDRESS, ""http://www.google.com"");
startActivity(i);
Add it in Manifest file -
<uses-permission android:name="android.permission.INTERNET" />
I need to load a PDF file within my webview on a button click within it. I have used google docs URL for loading the same. It works perfectly in S8 edge(v9.0) but it's not working in S6 edge(v6.0.1). It is not calling the shouldOverrideUrlLoading method even. Not able to understand what is the exact reason for it.
My code:
private final String googleDocs = "http://docs.google.com/gview?embedded=true&url=";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (request.getUrl().toString().endsWith("pdf")) {
String pdfUrl = googleDocs + request.getUrl().toString();
view.loadUrl(pdfUrl);
} else {
view.loadUrl(request.getUrl().toString());
}
return true;
}
});
WebSettings webSettings = binding.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
This site works well in android browsers but it doesn't work in android webview.
I set internet permission and did all things.
Who can teach me?
public class MainActivity extends AppCompatActivity {
WebView web_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.webView);
getSupportActionBar().hide();
web_view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
web_view.getSettings().setDomStorageEnabled(true);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebViewClient(mWebViewClient);
web_view.addJavascriptInterface(new MyJavaScriptInterface(), "android");
web_view.loadUrl("http://182.72.55.182/pms/employee");
}
WebViewClient mWebViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
#JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
}
make sure that your regarding website becoming live in everywhere!
webview doesn't display any local host website
Try to put WebViewClient part inside the onCreate.
I have an activity which is displaying a web page using a WebView. Within that page, there is a link to a YouTube video (so it's not a video I can or need to embed).
The problem is that the video won't play - When i click on the play button, a error page appears saying
" Webpage not available
The webpage at vnd.youtube:SVf8Ghl6d8xx
might be temporarily down and blah blah blah !!!"
Please only answer if you know what i want! I have gone through almost all related posts on stackoverflow so there is no need of references to other posts/question.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add the custom view to the action bar
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
webView.getSettings().setPluginState(WebSettings.PluginState.ON); //Helps flash run on a device
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient ());
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().setUseWideViewPort(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
}
The following code make it run in the youtube app but i want the videos to run in my app :(
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add the custom view to the action bar
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2)
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient () {
# Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("vnd.youtube")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else return false;
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setUseWideViewPort(true);
//webView.getSettings().setUseWideViewPort(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(url);
}
Go to Google Developer Console and select or create a new project.
On the left sidebar, select APIs under APIs & auth and turn the status ON for YouTube Data API v3.
On the left sidebar, select Credentials and Create new key under Public API access.
When popup comes asking you to choose platform, select Android Key.
Paste the SHA-1 key and your project’s package name separated by semicolon(;).
Click on create. Now you should see the API KEY on the dashboard.
download YouTubeAPI jar from here
https://developers.google.com/youtube/android/player/downloads/
Create a layout like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:layout_centerInParent="true" />
</RelativeLayout>
Your activity should look something like this
public class CustomYouTubePlayer extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
private String API_KEY="your key";
private String VIDEO_ID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** attaching layout xml **/
setContentView(R.layout.youtube_player);
/** Initializing YouTube player view **/
VIDEO_ID = getIntent().getExtras().getString(FinalVariables.YOUTUBE_ID);
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
player.setShowFullscreenButton(false);
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.loadVideo(VIDEO_ID);
}
}
private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(YouTubePlayer.ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
finish();
}
#Override
public void onVideoStarted() {
}
};
}
Just pass the video id and that's all simple as that..
Edit
if this is your YouTube link https://www.youtube.com/watch?v=rql_F8H3h9E
then your video_id=rql_F8H3h9E
Extract your video id from YouTube link and sent to this activity as extra variable.
The Best Way you could embed youtube videos in the app is by using Youtube API
The YouTube Android Player API enables you to incorporate video
playback functionality into your Android applications. The API defines
methods for loading and playing YouTube videos (and playlists) and for
customizing and controlling the video playback experience.
For this you will convert the iFrame code of your embed YouTube video to a string and load it to webview as a string in your application.
For example,
String frameVideo = "<html><body>Youtube video .. <br> <iframe width="320" height="315" src="https://www.youtube.com/embed/lY2H2ZP56K4" frameborder="0" allowfullscreen></iframe></body></html>";
then load it to your webview after all the normal webview settings
WebView displayVideo = (WebView)findViewById(R.id.webView);
displayVideo.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayVideo.loadData(frameVideo, "text/html", "utf-8");
For more information refer this links 1 2 3
i have a problem with loading a webview from a string url.
here is the code of an activity with only WebView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Intent intent=getIntent();
String url=intent.getStringExtra("url");
//EditText edit=(EditText)findViewById(R.id.editText1);
//String url=edit.getText().toString();
WebView webview=(WebView)findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new WebViewClient());
}`
and code of the calling activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onGo(View v)
{
EditText edit=(EditText)findViewById(R.id.editText1);
Intent intent=new Intent(MainActivity.this,WebActivity.class);
intent.putExtra("url", edit.getText().toString());
startActivity(intent);
}
i have even added the android.permissions.INTERNET in the manifest
but still i cannot view the page with the click of the button in MainActivity.
From the comment
yes the real device has the same error with "www.google.com"
Use
http://www.google.com
Also make sure you have internet permission mentioned in manifest file
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}