Last active
January 28, 2019 14:39
-
-
Save mzaini30/e7529195719138ce213e1660d1ba5b72 to your computer and use it in GitHub Desktop.
Main activity java dengan webview dan admob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mzaini30.onedayonepost; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.content.Context; | |
import android.view.KeyEvent; | |
import android.webkit.URLUtil; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.webkit.CookieManager; | |
import androidx.appcompat.app.AppCompatActivity; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.AdView; | |
import com.google.android.gms.ads.MobileAds; | |
public class MainActivity extends AppCompatActivity { | |
private AdView mAdView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// AdMob banner ad test id: ca-app-pub-3940256099942544/6300978111 | |
// my banner ad id: ca-app-pub-2878374163061282/6615438108 | |
// change to your own ad id | |
// Change in MainActivity.java and activity_main.xml | |
MobileAds.initialize(this, "ca-app-pub-2238217504982060/3753994093"); | |
mAdView = (AdView)findViewById(R.id.adView); | |
AdRequest adRequest = new AdRequest.Builder().build(); | |
mAdView.loadAd(adRequest); | |
// https://stackoverflow.com/questions/44324373/cookies-are-not-being-stored-in-android-webview | |
CookieManager cookieManager = CookieManager.getInstance(); | |
cookieManager.setAcceptCookie(true); | |
cookieManager.acceptCookie(); | |
cookieManager.setAcceptFileSchemeCookies(true); | |
WebView webview = (WebView) findViewById(R.id.webview); | |
WebSettings webSettings = webview.getSettings(); | |
webSettings.setJavaScriptEnabled(true); | |
webSettings.setDatabaseEnabled(true); | |
webSettings.setAllowFileAccess(true); | |
webSettings.setAllowContentAccess(true); | |
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); | |
webSettings.setDatabasePath(databasePath); | |
webSettings.setDomStorageEnabled(true); | |
// setContentView(webview); | |
webview.setWebViewClient(new WebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
// TODO Auto-generated method stub | |
if (url.contains("https://api.whatsapp.com") || url.contains("https://play.google.com")){ | |
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
startActivity(i); | |
} else { | |
view.loadUrl(url); | |
} | |
return true; | |
} | |
}); | |
// replace this with your own link/web app address | |
if (savedInstanceState == null) { | |
webview.loadUrl("file:///android_asset/index.html"); | |
} | |
} | |
@Override | |
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
WebView webview = (WebView) findViewById(R.id.webview); | |
if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
switch (keyCode) { | |
case KeyEvent.KEYCODE_BACK: | |
if (webview.canGoBack()) { | |
webview.goBack(); | |
} else { | |
finish(); | |
} | |
return true; | |
} | |
} | |
return super.onKeyDown(keyCode, event); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState ) | |
{ | |
WebView webview = (WebView) findViewById(R.id.webview); | |
super.onSaveInstanceState(outState); | |
webview.saveState(outState); | |
} | |
@Override | |
protected void onRestoreInstanceState(Bundle savedInstanceState) | |
{ | |
WebView webview = (WebView) findViewById(R.id.webview); | |
super.onRestoreInstanceState(savedInstanceState); | |
webview.restoreState(savedInstanceState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment