i want to display Interstitial onclick or open url ..
in webview
plz help me
my ShowWebViev.java :
public class ShowWebView extends Activity {
//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
AdView mAdView = (AdView) findViewById(R.id.adView );
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
startWebView("http://MYsite");
}
public class ShowWebView extends Activity {
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-555555555555555/12334567890");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("rtmp:") | url.endsWith(".m3u8") | url.startsWith("http://212.35.75.130")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("com.xmtvplayer.watch.live.streams", "org.zeipel.videoplayer.XMTVPlayer"));
intent.setData(Uri.parse(url));
intent.putExtra("secure_uri", true);
startActivity(intent);
return true;
} else{
Toast.makeText(getApplicationContext(), "tv", Toast.LENGTH_SHORT).show(); }
return false;
}
/*/Show loader on url load
public void onLoadResource (WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(ShowWebView.this);
progressDialog.setMessage("Loading... plz download XMTV Player To Watch Channel");
progressDialog.show();
}
}*/
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
startWebView("file:///android_asset/myerrorpage.html");
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webView.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
webView.clearHistory();
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.ref){
webView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
Regarding docs of xmtv player, you can use startActivityForResult instead of startActivity. You can call your Interstitial Ad on event onActivityResult.
public void XMTVPlayerPlayUri(String Title,String uri){
// [String] adszone - get zone id after filling form.
// [Integer] adstime - how long ad will stay before user will able to close it, value is in seconds.
Bundle bnd = new Bundle();
bnd.putString("path", uri);
bnd.putString("name", Title);
bnd.putString("adszone", "15E56089-CB15-4AA7-8D1B-574E899DE009"); // change adszone with your own id.
bnd.putInt("adstime",30);
Intent intent = new Intent();
intent.setClassName("com.xmtvplayer.watch.live.streams","org.zeipel.videoplayer.XMTVPlayer");
intent.putExtras(bnd);
startActivityForResult(intent, 100);
}
Related
I have placed an interstitial with click on back button in my Web View app. However , back button doesn't work when ad is not available. However, it seems to work fine after an interstitial ad has been loaded once. I am not a professional developer. Any help is much appreciated. Thank you.
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//admobinterstital
MobileAds.initialize(this, new OnInitializationCompleteListener() {
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
public void onAdLoaded(InterstitialAd interstitialAd) {
InterstitialAd unused = MainActivity2.this.mInterstitialAd = interstitialAd;
Log.i("TAG", "onAdLoaded");
}
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.i("TAG", loadAdError.getMessage());
InterstitialAd unused = MainActivity2.this.mInterstitialAd = null;
}
});
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
});
//WEB VIEW SETTINGS
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
//LIST VIEW
int position = getIntent().getIntExtra("story_key", 0);
//FOR MORE URL
}
// ENABLE BACK PRESS
public void onBackPressed() {
InterstitialAd interstitialAd = this.mInterstitialAd;
if (interstitialAd != null) {
interstitialAd.show(this);
if (this.webView.canGoBack()) {
this.webView.goBack();
} else {
super.onBackPressed();
}
}
}
}
//Declaration
public class DealerLoginActivity extends AppCompatActivity {
WebView webView;
ProgressBar progressBar;
AdView mAdView;
String url = "http://farmech.bih.nic.in/FMNEW/MDealerLoginNew.aspx";
// Initialization
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dealer_login);
webView = findViewById(R.id.web_views);
new MyAsynTask().execute();
progressBar = findViewById(R.id.progress_login);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-7657536360406007/9152366791");
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
// Using Jsoup library for removing Header, Nav and Footer.
#SuppressLint("StaticFieldLeak")
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
#Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document = Jsoup.connect(url).get();
document.getElementsByTag("header").remove();
document.getElementsByTag("nav").remove();
document.getElementsByTag("footer").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
//After background task Success post Execute show all the data.
#Override
protected void onPostExecute(final Document document) {
super.onPostExecute(document);
webView.loadDataWithBaseURL(url, document.toString(), "text/html", "utf-8", "");
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
}
}
}
// This code works fine for opening Url, didn't show any footer,header and nav,(https://i.stack.imgur.com/j3ZIp.jpg ) but after clicking on submit or any other link which are available in WebView(url) then it will show header,nav and footer ( https://i.stack.imgur.com/ByAgz.jpg ) how can i fix this .....how to remove permanent remove from webview
Probably an approach could be override onPageFinished method and inject javascript in order to remove these html elements.
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header\"); header.parentNode.removeChild(header);");
}
And make sure you have enable javascript
webSettings.setJavaScriptEnabled(true);
I want to open Play Store if user taps link of this type: market://I tried using getUrl() but it fetches URL only for the first time, not when user taps further links inside webView.
Here is my code:
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
swipeLayout.setOnRefreshListener(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webview = (WebView) findViewById(R.id.dashboard);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.loadUrl("http://example.test");
webview.setWebViewClient(new WebViewClient());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
progressBar.setProgress(0);
webview.setWebChromeClient(new WebChromeClient());
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
super.onProgressChanged(view, progress);
}
});
I made it to work like this:Created new SubClass:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
}
Also Added Subclass in webview:
webview.setWebViewClient(new HelloWebViewClient());
hi guys im getting this error in my log cat and i dont know how to fix..
android.app.ServiceConnectionLeaked: Activity com.example.harrops.h20droidapp.webstreamsListview has leaked ServiceConnection azy#236e1451 that was originally bound here
i have a list view that when an item is clicked it opens a webview for that item. i also have a webclient set up for this as i need the webpage have its own custom loading dialog and also if it opens certain links it will open them in the factory browser. any help or incite into what is going on would be greatly appreciated
this is my webstreamsListview
#Override
public void onSaveInstanceState(Bundle outState) {
wv.saveState(outState);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webstreams_listview);
if (savedInstanceState == null) {
mAdView = (AdView) findViewById(R.id.webstreams_adview);
mAdView2 = (AdView) findViewById(R.id.webstreams_adview1);
lv = (ListView) findViewById(R.id.lvWebstreamingSites);
lv.setScrollbarFadingEnabled(false);
lv.setAdapter(new webStreamsAdapter(this));
wv = (WebView) findViewById(R.id.wvWebStreamingSites);
final AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mAdView.loadAd(adRequest);
lv.setOnItemClickListener(new CastingOnitemListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
wv.setScrollbarFadingEnabled(false);
wv.getSettings().setJavaScriptEnabled(true);
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.NORMAL);
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
if (Build.VERSION.SDK_INT >= 19) {
// chromium, enable hardware acceleration
wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
wv.canGoBackOrForward(1);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.setPadding(0, 0, 0, 0);
wv.setWebViewClient(new webclient1(dialog, c));
wv.loadUrl(MY URL);
break;
and this is my webclient class
public class webclient1 extends WebViewClient {
View v;
ProgressDialog dg;
public webclient1(ProgressDialog dialog, Context c) {
this.dg = dialog;
dg = new ProgressDialog(c,R.style.CustomDialog);
dg.setTitle("H20 its an app thing ...");
dg.setIcon(R.drawable.h20_dialog_logo);
dg.setMessage("Please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String urlNewString) {
webView.getSettings().setSupportMultipleWindows(true);
webView.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(urlNewString)));
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.setVisibility(View.GONE);
dg.show();
}
#Override
public void onPageFinished(WebView view, String url) {
view.setVisibility(View.VISIBLE);
dg.dismiss();
}
}
ok guys i think it was because of my custom webclient still passing services after the webview was closed this is how ive fixed it.
i did this void method in my webstreamslistview class which clears everything once the webpage is destroyed
#Override
protected void onDestroy() {
super.onDestroy();
wv.removeAllViews();
wv.clearHistory();
wv.clearCache(true);
wv.clearView();
wv.destroy();
wv= null;
}
Simple code:
public class ZSEEActivity extends TabActivity {
private WebView webview ;
private WebView webviewtwo;
private TabHost mTabHost;
private int a;
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
protected void onStop() {
super.onStop();
// The activity is about to become visible.
}
protected void onRestart() {
super.onRestart();
}
protected void onDestroy(){
super.onDestroy();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Activity activity = this;
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Zastępstwa").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Plan Lekcji").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("O programie").setContent(R.id.tab3));
webview = (WebView) findViewById(R.id.webView1);
webviewtwo = (WebView) findViewById(R.id.webView2);
final WebSettings webviewtwoSettings = webviewtwo.getSettings();
if (savedInstanceState != null){
webview.restoreState(savedInstanceState.getBundle("stateone"));
webviewtwo.restoreState(savedInstanceState.getBundle("statetwo"));
webviewtwoSettings.setTextSize(TextSize.LARGER);
mTabHost.setCurrentTab(savedInstanceState.getInt("CURRENT_TAB"));
}
else{
webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
webviewtwoSettings.setTextSize(TextSize.LARGER);
mTabHost.setCurrentTab(0);
}
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
webview.loadData(summary, "text/html","utf-8");
Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
}
});
webviewtwo.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
String summary = "<html><body><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ><center>Coś się zepsuło :(</center></body></html>";
webviewtwo.loadData(summary, "text/html","utf-8");
webviewtwoSettings.setTextSize(TextSize.NORMAL);
Toast.makeText(activity, "O nie! " + description, Toast.LENGTH_SHORT).show();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
protected void onSaveInstanceState(Bundle outState) {
Bundle outStateone = new Bundle();
Bundle outStatetwo = new Bundle();
webview.saveState(outStateone);
webviewtwo.saveState(outStatetwo);
outState.putBundle("stateone", outStateone);
outState.putBundle("statetwo", outStatetwo);
outState.putInt("CURRENT_TAB", mTabHost.getCurrentTab());
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
final AlertDialog alertdialog= new AlertDialog.Builder(this).create();
alertdialog.setTitle("O Programie");
alertdialog.setMessage("Zmiany w 1.0.1: \n-Obsługa planu z dnia 17.10.2011\n-Drobne Poprawki");
alertdialog.setButton("Fajno", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
alertdialog.cancel();
}
});
alertdialog.show();
return true;
case R.id.item2:
finish();
case R.id.item3:
if(mTabHost.getCurrentTab() == 0){
webview.loadUrl("http://zsee.bytom.pl/ogloszenia.php");
}
else if(mTabHost.getCurrentTab() == 1)
{
webviewtwo.loadUrl("http://zsee.bytom.pl/plannew/index.html");
}
default:
return super.onOptionsItemSelected(item);
}
}
}
Now My problem. After i press back button onStop() code is executed and onDestroy. How i can don't kill app ? I wanna this app in background. Now when i press back button and open app, all data is again downloaded and loaded to webview. Soo how make this process work in background ?
Sorry for my haotic english :)
Sierran
Use Android service for doing something in Background rather then Activity.
And use Broadcast receiver to invoke your Activity from service. When something your background work finished.
Android - Service
And if you want to do some little then just override onKeyDown()
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// put your stuff here or just block the back button for perticular activity
return true;
}
return super.onKeyDown(keyCode, event);
}
I would recommend using a Service if you want to do something in the background and need it to be running more or less all the time. Check out the Service documentation here:
http://developer.android.com/reference/android/app/Service.html
An alternative in your case would be to create local caches of the websites.