package com.example.webviewtest;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import android.support.v7.app.ActionBarActivity;
import android.widget.LinearLayout;
@EActivity(R.layout.activity_main)
public class MainActivity extends ActionBarActivity {
MainFragment mainFrgm;
LinearLayout webViewLayout;
@AfterViews
void initViews() {
mainFrgm = new MainFragment_();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, mainFrgm).commit();
// Webviewの表示先URL
mainFrgm.initWebView(this, "http://aaa.bbb.ccc/sample.html");
}
}
package com.example.webviewtest;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.webkit.HttpAuthHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment {
@ViewById
LinearLayout webViewLayout;
WebView webView;
Activity act;
String target_url;
@AfterViews
void initViews() {
webView = new WebView(act);
webViewLayout.addView(webView);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onLoadResource(WebView view, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
// in.setDataAndType(Uri.parse(url), "video/*");
startActivity(in);
return;
} else {
super.onLoadResource(view, url);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.endsWith(".mp4")){
Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
// in.setDataAndType(Uri.parse(url), "video/*");
startActivity(in);
return true;
}
else {
return false;
}
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.loadUrl(target_url);
}
void initWebView(Activity _act, String url)
{
target_url = url;
act = _act;
}
}
------
・activity_main(レイアウト)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.webviewtest.MainActivity_"
tools:ignore="MergeRootFrame" >
</FrameLayout>
---------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webviewtest.MainActivity_$PlaceholderFragment_" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="80sp" >
</ListView>
<LinearLayout
android:id="@+id/webViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
---------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<activity
android:name="com.example.webviewtest.MainActivity_"
android:label="@string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
---------
・sample.html(webviewで表示しているhtml)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>動画再生テスト</title>
</head>
<body>
<p>HTML5のvideoタグによる動画再生</p>
<video controls width="320" height="240" id="myVideoFile">
<source src="../mp4/sample.mp4">
<p>動画を再生するには、videoタグをサポートしたブラウザが必要です。</p>
</video>
</body>
</html>
---------
--
このメールは Google グループのグループ「日本Androidの会」に登録しているユーザーに送られています。
このグループから退会し、グループからのメールの配信を停止するには android-group-j...@googlegroups.com にメールを送信してください。
このグループに投稿するには android-g...@googlegroups.com にメールを送信してください。
http://groups.google.com/group/android-group-japan からこのグループにアクセスしてください。
その他のオプションについては https://groups.google.com/d/optout にアクセスしてください。
このグループから退会し、グループからのメールの配信を停止するには android-group-japan+unsub...@googlegroups.com にメールを送信してください。
package com.example.webviewtest;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.HttpAuthHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;
@EActivity(R.layout.activity_main)
public class MainActivity extends ActionBarActivity {
private VideoEnabledWebChromeClient webChromeClient;
@ViewById
VideoEnabledWebView webView;
@ViewById
View nonVideoLayout;
@ViewById
ViewGroup videoLayout;
@ViewById
ListView listView;
ActionBarActivity act;
boolean isFullScreen;
@SuppressLint("NewApi")
@AfterViews
void initViews() {
act = this;
View loadingView = getLayoutInflater().inflate(R.layout.video_loading_progress, null); // Your own view, read class comments
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
{
// Subscribe to standard events, such as onProgressChanged()...
@Override
public void onProgressChanged(WebView view, int progress)
{
// Your code...
}
};
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
@Override
public void toggledFullscreen(boolean fullscreen)
{
// Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
if (fullscreen)
{
isFullScreen = fullscreen;
// フルスクリーンの時は横対応あり
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
listView.setVisibility(View.GONE); // フルスクリーンの時はリストを消去
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
else
{
isFullScreen = false;
// フルスクリーン以外は縦固定
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
listView.setVisibility(View.VISIBLE); // フルスクリーンでなくなったらリストを表示
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
if (android.os.Build.VERSION.SDK_INT >= 14)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
}
});
webView.setWebChromeClient(webChromeClient);
// Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
webView.loadUrl("http://aaa.bbb.ccc/sample.html");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
if (isFullScreen != true) {
// フルスクリーン以外は縦固定
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else {
// フルスクリーンの時は横対応あり
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
}
super.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed()
{
// Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
if (!webChromeClient.onBackPressed())
{
if (webView.canGoBack())
{
webView.goBack();
}
else
{
// Close app (presumably)
super.onBackPressed();
}
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.webviewtest.MainActivity_"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="80sp" >
</ListView>
<!-- View that will be hidden when video goes fullscreen -->
<RelativeLayout
android:id="@+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/listView" >
<com.example.webviewtest.VideoEnabledWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<!-- View where the video will be shown when video goes fullscreen -->
<RelativeLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/listView"
>
<!-- View that will be shown while the fullscreen video loads (maybe include a spinner and a "Loading..." message) -->
<View
android:id="@+id/videoLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"
/>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
<activity
android:name="com.example.webviewtest.MainActivity_"
android:configChanges="orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
からす様情報有り難うございます。
2014年9月27日土曜日 13時27分19秒 UTC+9 karasu:
...<p style="margin-bottom:0px;font-size:11px;lin