2016年7月24日日曜日

Visual Studio 2015 with Xamarin AndroidでNendのネイティブアドの広告コードをWebViewに

参考URL:http://lab.adn-mobasia.net/?p=2064

AndroidアプリでnendSDKが使えなくてもnend広告を表示する方法

より


詰まった点

mWebViewNendAd.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
 startActivity(intent);
return true;
 }
} );



Xamarin では


呼び出し

mWebViewNendAd.SetWebViewClient(new myWebViewClient(Activity));

プライベートクラス


private class myWebViewClient : WebViewClient
        {
            Context context;
            public myWebViewClient(Context context)
            {
                this.context = context;
            }
            public override bool ShouldOverrideUrlLoading(WebView view, string _url)
            {
                var url = Android.Net.Uri.Parse(_url);
                var intent = new Intent(Intent.ActionView, url);
                context.StartActivity(intent);
                return true;
            }
        }

contextを渡せば良いだけだった。

Visual Studio 2015 with Xamarin AndroidでWebViewにProgressBarを追加する

Point XML

RelativeLayoutでwebviewを先に置いて、ProgressBarで上書き


Point source

SetWebChromeClientにoverrideしたプライベートクラスを適応し、
そのクラスにProgressBarを渡しているだけ


気になること
ProgressBarを渡したけど、問題は出ないのだろうか?
こういうのを確かめないからいつまでもコピペ。

追記
2016/07/25
問題でてた。タップされるまで動画が表示されない。
いったん修正。

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/DIframeLayout">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/FwebView"
        android:layout_centerInParent="true" />
  <ProgressBar
    android:id="@+id/FprogressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</RelativeLayout>

ソース

//…省略…/
public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Bundle savedInstanceState)
        {
            var root = inflater.Inflate(Resource.Layout.fragmentWebView, container, false);
            fWebView = root.FindViewById<WebView>(Resource.Id.FwebView);
            fProgress = root.FindViewById<ProgressBar>(Resource.Id.FprogressBar);

//…省略…/
 fWebView.SetWebChromeClient(new myWebChromeClient(fProgress));

//…省略…/
return root;

//…省略…/

 public class myWebChromeClient : WebChromeClient
        {
            private ProgressBar fProgress = null;
            public myWebChromeClient(ProgressBar setProgress) : base()
            {
                this.fProgress = setProgress;
            }

            public override void OnProgressChanged(WebView view, int newProgress)
            {
                if (fProgress != null)
                {
                    fProgress.Progress = newProgress;
                    if (newProgress == 100)
                    {
                     fProgress.Animate().SetDuration(1000).Alpha(0.0f);
                    }
                    else
                    {
                        fProgress.Alpha = 1.0f;
                    }
              }
          }

Android SDK manual download and offline install

First step

request 1 : Acsess to Internet by PC_1[download only]
request 2 : offline install by target PC_2

Install

・JDK download and install

・Android SDK [https://developer.android.com/studio/index.htm]
download [installer_r??.?.?-***.exe] and install by target PC

Second

targetPC\...\Android\android-sdk\

make folder [temp]


Thrid

Acsess and Download

Acsess
[http://dl-ssl.google.com/android/repository/repository-12.xml]
or
[http://dl-ssl.google.com/android/repository/repository-1?.xml]   ?= 3,4,5,...

[Ctrl + F] -> ".zip</"

ex. tools_r25.1.7-windows.zip

Download

http://dl-ssl.google.com/android/repository/[Find name]

ex. [http://dl-ssl.google.com/android/repository/tools_r25.1.7-windows.zip]

Fourth

Download File to [temp]
targetPC\...\Android\android-sdk\temp

Fifth

SDK Manager.exe 

1.check download file name
2.check box
3.Install button

complete.

Visual Studio2015 with Xamarin Androidはオフラインインストール出来ない

https://msdn.microsoft.com/library/e2h7fzkw.aspx
にあるように、AndroidSDKはオフラインインストールに対応していない。

なので、同じページにあるVS2015のコマンドラインパラメータの使用にある

「/layout Directory」:インストール メディアのファイルを指定したディレクトリにコピーします。

を使っても、意味がない。
AndroidSDKを先にインストールしていても、VS2015と紐づけするためにダウンロードされたので、
オフラインインストールはできなかった。

実際のコマンド