我正在制作自己的YouTube频道的应用程序,但是当我运行该应用程序时,我会显示youtube的桌面版本。但是,当我放置博客之类的网站https://atifsoftwares.blogspot.com的网址时,它会显示移动版本。 我该如何解决这个问题?
这是屏幕截图:
Youtube频道的另一个屏幕截图:
这是我的代码:
package com.blogspot.atifsoftwares.theurduteacher;
import android.Manifest;
import android.app.DownloadManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ObservableWebView;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
public class MainActivity extends AppCompatActivity {
//private String current_page_url ="https://atifsoftwares.blogspot.com/";
private String current_page_url ="https://www.youtube.com/c/MohammadAtif1";
private ObservableWebView mWebView;
private ProgressBar mPrgrsBar;
private FrameLayout mFrmLayout;
private static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 1;
//for bookmarks
public static final String PREFERENCES = "PREFERENCES_NAME";
public static final String WEB_LINKS = "links";
public static final String WEB_TITLE = "title";
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
//permission for Write_External_storage for downloading
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
}
}
//get url from bookmark(when a url from bookmark activity is clicked)
if (getIntent().getExtras() != null) {
current_page_url = getIntent().getStringExtra("url");
}
//horizontal progressbar
mFrmLayout = findViewById(R.id.frameLayout);
mPrgrsBar = findViewById(R.id.progressBar);
mPrgrsBar.setMax(100);
//implementing WebView
mWebView = (ObservableWebView) findViewById(R.id.webView);
mWebView.setWebViewClient(new HelpClient());
mWebView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress){
mFrmLayout.setVisibility(View.VISIBLE);
mPrgrsBar.setProgress(progress);
setTitle("Loading...");
if (progress==100){
mFrmLayout.setVisibility(View.GONE);
setTitle(view.getTitle());
}
super.onProgressChanged(view, progress);
}
});
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setUserAgentString("Mozilla/5.0");
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setVerticalScrollBarEnabled(true);
//Show/Hide ActionBar & Statusbar while scrolling
mWebView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
if (scrollState == ScrollState.UP) {
actionBar.hide();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else if (scrollState == ScrollState.DOWN) {
actionBar.show();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
});
//for downloading image onLongClick
registerForContextMenu(mWebView);
//check internet connection
if(haveNetworkConnection()){
mWebView.loadUrl(current_page_url);
} else {
Toast.makeText(MainActivity.this, "Error! Check Internet Connection", Toast.LENGTH_SHORT).show();
mWebView.loadUrl(current_page_url);
}
mPrgrsBar.setProgress(0);
//fav menu for copy/share link of opened page
FabSpeedDial mFabMenu = (FabSpeedDial)findViewById(R.id.fabMenus);
mFabMenu.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(FloatingActionButton fab, TextView textView, int itemId) {
if (itemId==R.id.copyIt){
String s = mWebView.getUrl();
ClipboardManager cb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cb.setText(s);
Toast.makeText(getApplicationContext(), "Link Copied...", Toast.LENGTH_SHORT).show();
}
if (itemId==R.id.shareIt){
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = mWebView.getUrl();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
}
});
//handle downloading
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading File...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}});
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onPause() {
mWebView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mWebView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mWebView.destroy();
mWebView = null;
super.onDestroy();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
final WebView.HitTestResult webViewHitTestResult = mWebView.getHitTestResult();
if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
contextMenu.setHeaderTitle("Download");
contextMenu.setHeaderIcon(R.drawable.ic_action_downimg);
contextMenu.add(0, 1, 0, "Save - Download Image")
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String DownloadImageURL = webViewHitTestResult.getExtra();
if(URLUtil.isValidUrl(DownloadImageURL)){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(MainActivity.this,"Downloading Image....",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"Sorry.. Something Went Wrong.",Toast.LENGTH_LONG).show();
}
return false;
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
String links = sharedPreferences.getString(WEB_LINKS, null);
if (links != null) {
Gson gson = new Gson();
ArrayList<String> linkList = gson.fromJson(links, new TypeToken<ArrayList<String>>() {}.getType());
if (linkList.contains(current_page_url)) {
menu.getItem(1).setIcon(R.drawable.ic_action_booked);
} else {
menu.getItem(1).setIcon(R.drawable.ic_action_book);
}
} else {
menu.getItem(1).setIcon(R.drawable.ic_action_book);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_bookmark) {
addRemoveBookmark();
invalidateOptionsMenu();
}
if (id == R.id.refresh) {
if(haveNetworkConnection()){
mWebView.reload();
} else {
Toast.makeText(MainActivity.this, "Error! Check Internet Connection...", Toast.LENGTH_SHORT).show();
mWebView.reload();
}
return true;
}
if (id == R.id.action_bookmarkshow){
startActivity(new Intent(MainActivity.this, BookmarksActivity.class));
return true;
}
if (id == R.id.act_about) {
startActivity(new Intent(this, AboutActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
private void addRemoveBookmark() {
String message;
SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
String jsonLink = sharedPreferences.getString(WEB_LINKS, null);
String jsonTitle = sharedPreferences.getString(WEB_TITLE, null);
if (jsonLink != null && jsonTitle != null) {
Gson gson = new Gson();
ArrayList<String> linkList = gson.fromJson(jsonLink, new TypeToken<ArrayList<String>>() {}.getType());
ArrayList<String> titleList = gson.fromJson(jsonTitle, new TypeToken<ArrayList<String>>() {}.getType());
if (linkList.contains(current_page_url)) {
linkList.remove(current_page_url);
titleList.remove(mWebView.getTitle().trim());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(WEB_LINKS, new Gson().toJson(linkList));
editor.putString(WEB_TITLE, new Gson().toJson(titleList));
editor.apply();
message = "Bookmark Removed";
} else {
linkList.add(current_page_url);
titleList.add(mWebView.getTitle().trim());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(WEB_LINKS, new Gson().toJson(linkList));
editor.putString(WEB_TITLE, new Gson().toJson(titleList));
editor.apply();
message = "Bookmarked";
}
} else {
ArrayList<String> linkList = new ArrayList<>();
ArrayList<String> titleList = new ArrayList<>();
linkList.add(current_page_url);
titleList.add(mWebView.getTitle());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(WEB_LINKS, new Gson().toJson(linkList));
editor.putString(WEB_TITLE, new Gson().toJson(titleList));
editor.apply();
message = "Bookmarked";
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private class HelpClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
current_page_url = url;
invalidateOptionsMenu();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
mFrmLayout.setVisibility(View.VISIBLE);
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mWebView.loadUrl(request.getUrl().toString());
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
invalidateOptionsMenu();
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(MainActivity.this, "Something went wrong!\nCheck internet connection...", Toast.LENGTH_SHORT).show();
invalidateOptionsMenu();
}
}
}
答案 0 :(得分:1)
您不要求使用移动版本来做
更改
https://www.youtube.com/c/MohammadAtif1
收件人
https://m.youtube.com/c/MohammadAtif1
并在第
行下方添加webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");