package com.weikou.beibeivideo.ui.common;
|
|
import android.app.Notification;
|
import android.app.NotificationChannel;
|
import android.app.NotificationManager;
|
import android.graphics.BitmapFactory;
|
import android.graphics.Color;
|
import android.graphics.PixelFormat;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import androidx.core.app.NotificationCompat;
|
import android.util.Log;
|
import android.view.KeyEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.webkit.DownloadListener;
|
import android.webkit.WebChromeClient;
|
import android.webkit.WebSettings;
|
import android.webkit.WebView;
|
import android.webkit.WebViewClient;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.lcjian.library.util.ManifestDataUtil;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.ui.BaseActivity;
|
import com.weikou.beibeivideo.util.downutil.DownFiles;
|
import com.weikou.beibeivideo.util.downutil.DownFiles.IProgress;
|
import com.weikou.beibeivideo.util.ui.TopStatusSettings;
|
|
public class LiveBrowserActivity extends BaseActivity implements OnClickListener {
|
private TextView tv_top_bar_left;
|
private TextView tv_top_bar_middle;
|
private WebView webview;
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
protected void onDestroy() {
|
webview.reload();
|
super.onDestroy();
|
}
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.live_browser_activity);
|
getWindow().setFormat(PixelFormat.TRANSLUCENT);
|
TopStatusSettings.setStatusViewAndDeepColor(this);
|
tv_top_bar_left = (TextView) findViewById(R.id.tv_top_bar_left);
|
tv_top_bar_middle = (TextView) findViewById(R.id.tv_top_bar_middle);
|
webview = (WebView) findViewById(R.id.webview);
|
tv_top_bar_left.setText("返回");
|
tv_top_bar_left.setOnClickListener(this);
|
WebSettings webSettings = webview.getSettings();
|
webSettings.setJavaScriptEnabled(true);
|
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
webSettings.setDomStorageEnabled(true);
|
|
webview.setDownloadListener(new MyWebViewDownLoadListener());
|
|
webview.setWebViewClient(new WebViewClient() {
|
|
@Override
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
return super.shouldOverrideUrlLoading(view, url);
|
}
|
});
|
webview.setWebChromeClient(new WebChromeClient() {
|
|
@Override
|
public void onReceivedTitle(WebView view, String title) {
|
tv_top_bar_middle.setText(title);
|
}
|
});
|
webview.loadUrl(getIntent().getStringExtra("url"));
|
}
|
|
// 文件下载监听
|
private class MyWebViewDownLoadListener implements DownloadListener {
|
|
@Override
|
public void onDownloadStart(String url, String userAgent,
|
String contentDisposition, String mimetype, long contentLength) {
|
startDownLoadFile(url);
|
/*
|
* Uri uri = Uri.parse(url); Intent intent = new
|
* Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
|
*/
|
}
|
}
|
|
private NotificationManager manager;
|
private Notification notif;
|
private Notification.Builder oBuilder;
|
private NotificationCompat.Builder builder;
|
int j = -1;
|
|
private void startDownLoadFile(String url) {
|
|
new DownFiles(this, new IProgress() {
|
|
@Override
|
public void getProgress(int p) {
|
// stub
|
if (p > 99) {
|
handler.sendEmptyMessage(1);
|
Log.i("DownFiles", "getProgress下载进度:" + p);
|
} else {
|
if (manager == null) {
|
Toast.makeText(LiveBrowserActivity.this, "文件已经开始下载",
|
Toast.LENGTH_SHORT).show();
|
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
String id = "UMENG_CHANNEL";
|
String description = ManifestDataUtil.getAppMetaData(LiveBrowserActivity.this, id);
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
int importance = NotificationManager.IMPORTANCE_HIGH;
|
NotificationChannel mChannel = new NotificationChannel(id, "123", importance);
|
mChannel.setDescription(description);
|
mChannel.enableLights(true);
|
mChannel.setLightColor(Color.RED);
|
mChannel.enableVibration(true);
|
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
|
manager.createNotificationChannel(mChannel);
|
oBuilder = new Notification.Builder(LiveBrowserActivity.this, id);
|
oBuilder.setContentTitle("影视大全")
|
.setSmallIcon(R.drawable.ic_launcher)
|
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
|
.setContentText("热门应用下载")
|
.setAutoCancel(true)
|
.build();
|
} else {
|
builder = new NotificationCompat.Builder(LiveBrowserActivity.this);
|
builder.setContentTitle("影视大全")
|
.setContentText("热门应用下载")
|
.setSmallIcon(R.drawable.ic_launcher)
|
.setOngoing(true);//无效
|
}
|
}
|
|
Message msg = handler.obtainMessage();
|
if (j != p) {
|
msg.what = 0;
|
msg.arg1 = p;
|
handler.sendMessage(msg);
|
}
|
j = p;
|
}
|
}
|
}).execute(url);
|
}
|
|
private Handler handler = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
switch (msg.what) {
|
case 0:
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
oBuilder.setContentText(msg.arg1 + "%");
|
notif = oBuilder.build();
|
} else {
|
builder.setContentText(msg.arg1 + "%");
|
notif = builder.build();
|
}
|
manager.notify(0, notif);
|
break;
|
case 1:
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
oBuilder.setContentText("下载完成");
|
notif = oBuilder.build();
|
} else {
|
builder.setContentText("下载完成");
|
notif = builder.build();
|
}
|
manager.notify(0, notif);
|
j = 100;
|
manager.cancelAll();
|
break;
|
default:
|
break;
|
}
|
}
|
};
|
@Override
|
public void onClick(View v) {
|
switch (v.getId()) {
|
case R.id.tv_top_bar_left: {
|
if (!webview.canGoBack())
|
finish();
|
else {
|
webview.goBack();
|
}
|
}
|
break;
|
default:
|
break;
|
}
|
}
|
|
// 设置回退
|
// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
|
webview.goBack(); // goBack()表示返回WebView的上一页面
|
return true;
|
}
|
return super.onKeyDown(keyCode, event);
|
}
|
}
|