需要apk的可以私聊我,看到後會第一時間發給你!
最近聽歌好多喜歡聽的歌都需要vip,然後就研究了一個在線免費聽歌的APP,實際上沒用到啥技術,主要就是一個加載webView,喜歡的話點個贊吧。
1.app頁面樣式
2.主要代碼
package com.example.administrator.myapplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* @author xuwei
* on 2019/9/26
* 父類webview頁面
*/
public abstract class BaseWebViewActivity extends Activity {
private WebView activityWebview;
private WebSettings webSettings;
private ProgressBar activityWebviewProgress;
private LinearLayout mErrorFrame;
private Context context = this;
private TextView normalTittle, tvRightName;
private RelativeLayout rlTitle;
private RelativeLayout rlBack;
/**
* 加載視圖
*/
protected abstract void onInitView();
/**
* 加載完成監聽
*/
protected abstract void onLoadFinish();
private int sdkInt = 9;
private int progress = 100;
private String blank = "about:blank";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
if (Build.VERSION.SDK_INT > sdkInt) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
initView();
initInfo();
onInitView();
}
/**
* 初始化視圖
*/
private void initView() {
activityWebview = findViewById(R.id.activity_webview);
activityWebviewProgress = findViewById(R.id.activity_webview_progress);
mErrorFrame = findViewById(R.id.activity_erro_page);
normalTittle = findViewById(R.id.tvTitleName);
rlBack = findViewById(R.id.rlBack);
rlTitle = findViewById(R.id.rlTitle);
tvRightName = findViewById(R.id.tvRightName);
tvRightName.setText("打賞");
tvRightName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MoneyActivity.class);
startActivity(intent);
}
});
}
/**
* 加載url
*/
public void loadUrl(final String url) {
activityWebview.loadUrl(url);
}
/**
* 設置title是否顯示
*/
public void setTitle() {
rlTitle.setVisibility(View.VISIBLE);
}
/**
* 設置返回鍵
*/
public void back() {
rlBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
/**
* 設置標題名稱
*/
public void setHeadName(String name) {
normalTittle.setText(name);
}
@SuppressLint({"SetJavaScriptEnabled", "WrongConstant"})
private void initInfo() {
webSettings = activityWebview.getSettings();
//webview定位相關設置
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationEnabled(true);
//設置定位的數據庫路徑
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
webSettings.setGeolocationDatabasePath(dir);
//啓用數據庫
webSettings.setDatabaseEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setBlockNetworkImage(false);
String appCacheDir = getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
webSettings.setAppCachePath(appCacheDir);
webSettings.setSupportZoom(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
activityWebview.setScrollBarStyle(0);
activityWebview.setWebViewClient(new WebClient());
activityWebview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
}).show();
result.cancel();
return true;
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
return super.onJsConfirm(view, url, message, result);
}
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
return true;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress == progress) {
goneProgress();
}
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activityWebview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
public void goneProgress() {
activityWebviewProgress.setVisibility(View.GONE);
}
/**
* 監聽加載完成
*/
public class WebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
url = request.getUrl().toString();
} else {
url = request.toString();
}
try {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
return true;
} catch (Exception e) {
return false;
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (blank.equals(url)) {
mErrorFrame.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
}
/**
* 銷燬頁面
*/
@Override
protected void onDestroy() {
super.onDestroy();
activityWebview.destroy();
}
/**
* 返回鍵處理
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && this.activityWebview.canGoBack()) {
String url = this.activityWebview.getUrl().toString();
this.activityWebview.goBack();
if (this.activityWebview.getUrl().toString().equals(url)) {
this.activityWebview.goBack();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F7F7F7"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rlTitle"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="40dp"
android:visibility="gone"
android:elevation="10px"
android:background="#F8F8F8">
<RelativeLayout
android:id="@+id/rlBack"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/titlebar_back" />
</RelativeLayout>
<TextView
android:id="@+id/tvTitleName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="20dp"
android:singleLine="true"
android:textColor="#333333"
android:textSize="18sp" />
<TextView
android:id="@+id/tvRightName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:textColor="#333333"
android:textSize="14sp" />
<ImageView
android:id="@+id/ivRightLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:scaleType="fitCenter"
android:visibility="gone" />
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/activity_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/activity_webview_progress"
style="?android:attr/progressBarStyleLargeInverse"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true" />
</RelativeLayout>
<LinearLayout
android:id="@+id/activity_erro_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="暫無數據"
android:textColor="#999999"
android:textSize="18sp" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
需要apk的可以私聊我,看到後會第一時間發給你!