我对GooglePlayService(com.google.android.gms)有疑问 是否可以在没有GooglePlayService的情况下使用Youtube Data API?
我的设备未安装GooglePlayService和GooglePlayStore。 但我必须使用Youtube Data API。
现在使用Youtube Data API,显示错误对话框
'此应用无法在没有Google Play服务的情况下运行,而这些服务在手机中无法使用
请回复我的问题。 感谢。
答案 0 :(得分:0)
使用此片段在您的应用中显示Youtube视频。这个片段类允许: -
使用此布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/images"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<FrameLayout
android:id="@+id/customViewContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone" />
<LinearLayout
android:id="@+id/alert_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_info_white_48dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Connected to Network"
android:textColor="@color/actionbar_color"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
public class VideoFragment extends Fragment {
/** The web view. */
private WebView webView;
/** The custom view container. */
private FrameLayout customViewContainer;
/** The custom view callback. */
private WebChromeClient.CustomViewCallback customViewCallback;
/** The m custom view. */
private View mCustomView;
/** The m web chrome client. */
private myWebChromeClient mWebChromeClient;
/** The m web view client. */
private myWebViewClient mWebViewClient;
/**
* Instantiates a new video fragment.
*/
public VideoFragment() {
// Empty constructor required for fragment subclasses
}
/*
* (non-Javadoc)
*
* @see
* android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.media_fragment, container,
false);
customViewContainer = (FrameLayout) rootView
.findViewById(R.id.customViewContainer);
webView = (WebView) rootView.findViewById(R.id.webView);
if (!Connectivity.isConnected(getActivity())) {
webView.setVisibility(View.GONE);
rootView.findViewById(R.id.alert_root).setVisibility(View.VISIBLE);
final Dialog dialog = new Dialog((GoDhamHomeActivity) getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.connection_dialog);
Button dialogButton = (Button) dialog
.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
} else {
rootView.findViewById(R.id.alert_root).setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
mWebViewClient = new myWebViewClient();
webView.setWebViewClient(mWebViewClient);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSaveFormData(true);
webView.loadUrl("https://www.youtube.com/channel/UC8EbZkPWGtOn1YGVSUzjaWw/videos");
}
rootView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (inCustomView()) {
hideCustomView();
return true;
}
if ((mCustomView == null) && webView.canGoBack()) {
webView.goBack();
return true;
}
}
// return super.onKeyDown(keyCode, event);
return false;
}
});
return rootView;
}
/**
* In custom view.
*
* @return true, if successful
*/
public boolean inCustomView() {
return (mCustomView != null);
}
/**
* Hide custom view.
*/
public void hideCustomView() {
mWebChromeClient.onHideCustomView();
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onPause()
*/
@Override
public void onPause() {
super.onPause(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onPause();
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onResume()
*/
@Override
public void onResume() {
super.onResume(); // To change body of overridden methods use File |
// Settings | File Templates.
webView.onResume();
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.Fragment#onStop()
*/
@Override
public void onStop() {
super.onStop(); // To change body of overridden methods use File |
// Settings | File Templates.
if (inCustomView()) {
hideCustomView();
}
}
// public boolean onKeyDown(int keyCode, KeyEvent event) {
// if (keyCode == KeyEvent.KEYCODE_BACK) {
//
// if (inCustomView()) {
// hideCustomView();
// return true;
// }
//
// if ((mCustomView == null) && webView.canGoBack()) {
// webView.goBack();
// return true;
// }
// }
// return super.onKeyDown(keyCode, event);
// }
/**
* The Class myWebChromeClient.
*/
class myWebChromeClient extends WebChromeClient {
/** The m default video poster. */
private Bitmap mDefaultVideoPoster;
/** The m video progress view. */
private View mVideoProgressView;
/*
* (non-Javadoc)
*
* @see
* android.webkit.WebChromeClient#onShowCustomView(android.view.View,
* int, android.webkit.WebChromeClient.CustomViewCallback)
*/
@Override
public void onShowCustomView(View view, int requestedOrientation,
CustomViewCallback callback) {
onShowCustomView(view, callback); // To change body of overridden
// methods use File | Settings |
// File Templates.
}
/*
* (non-Javadoc)
*
* @see
* android.webkit.WebChromeClient#onShowCustomView(android.view.View,
* android.webkit.WebChromeClient.CustomViewCallback)
*/
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
webView.setVisibility(View.GONE);
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.addView(view);
customViewCallback = callback;
}
/*
* (non-Javadoc)
*
* @see android.webkit.WebChromeClient#getVideoLoadingProgressView()
*/
@Override
public View getVideoLoadingProgressView() {
if (mVideoProgressView == null) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
mVideoProgressView = inflater.inflate(R.layout.video_progress,
null);
}
return mVideoProgressView;
}
/*
* (non-Javadoc)
*
* @see android.webkit.WebChromeClient#onHideCustomView()
*/
@Override
public void onHideCustomView() {
super.onHideCustomView(); // To change body of overridden methods
// use File | Settings | File Templates.
if (mCustomView == null)
return;
webView.setVisibility(View.VISIBLE);
customViewContainer.setVisibility(View.GONE);
// Hide the custom view.
mCustomView.setVisibility(View.GONE);
// Remove the custom view from its container.
customViewContainer.removeView(mCustomView);
customViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
/**
* The Class myWebViewClient.
*/
class myWebViewClient extends WebViewClient {
/*
* (non-Javadoc)
*
* @see
* android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit
* .WebView, java.lang.String)
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url); // To change body
// of overridden
// methods use
// File |
// Settings |
// File
// Templates.
}
}
public static Fragment newInstance() {
// TODO Auto-generated method stub
return new VideoFragment();
}
}
使用此方法检查连接状态: -
public static boolean isConnected(Context context) {
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected());
}
这是用于显示离线通知的对话框布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/images"
android:layout_margin="5dp"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/godham_logo" />
<TextView
android:layout_centerHorizontal="true"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image"
android:text="Look like you are Offline. \n For better experience use Application online"
android:textColor="@color/actionbar_color"
android:textSize="20sp" />
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/green_g_plus_button"
android:text="Ok Got it"
android:textColor="#FFF"
android:textSize="20sp" />
</RelativeLayout>
我在github上有这个POC的托管项目,你可以在这里找到完整的项目
https://github.com/hiteshsahu/Android-Universal-Web-Content-Loader