2016年11月23日に「MainActivity.java」を更新いたしました。
2016年12月1日に「MainActivity.java」を更新いたしました。
Android Studio 2.1.0.0
使用端末:HTL22 Androidバージョン:4.1.2
ブラウザアプリの開発をしています。
やりたいことは、アプリ内のボタンを押すと、表示されているWebページを更新してすぐに更新中止という動作を高速で繰り返す事です。
以下の部分で「表示されているWebページを更新してすぐに更新中止」という動作を10回連続でさせているつもりなのですが、実際には1度だけ「更新→更新中止」がされているようです。
動作の確認は、cookieを無効にすると更新するごとにカウンタが増えるサイトで行いました。
実際には1秒間で15回ほど更新→更新中止をしたいと思っております。
・バージョンの違うAndroid端末でも同じ動作をさせるため、XWalkViewを使用しました。
・更新ボタン、更新中止ボタンを押すことでWebページの更新、更新中止の動作はできました。
どのようにすれば、表示されているWebページを更新してすぐに更新中止という動作を高速で繰り返す事ができるでしょうか。
何卒、ご教授をよろしくお願いいたします。
以下に、動作確認をした際の「MainActivity.java」「activity_main.xml」「AndroidManifest.xml」の内容を示します。
ご参照いただけますと幸いです。
MainActivity.java
java
1package パッケージ名 2 3import android.app.Activity; 4import android.app.ProgressDialog; 5import android.os.Bundle; 6import android.os.Handler; 7import android.view.View; 8import android.view.View.OnClickListener; 9import android.widget.Button; 10import android.widget.TextView; 11import android.support.v7.app.ActionBarActivity; 12import org.xwalk.core.XWalkView; 13import org.xwalk.core.internal.XWalkCookieManager; 14 15public class MainActivity extends Activity { 16 17 Handler mHandler; 18 private ProgressDialog progressDialog; 19 private XWalkView mXWalkView; 20 private XWalkCookieManager mCookieManager; 21 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 //mXWalkViewを作成 28 mXWalkView = (XWalkView) findViewById(R.id.xwalkWebView); 29 mXWalkView.load("対象のサイト", null); 30 31 //クッキーの設定 32 XWalkCookieManager mCookieManager = new XWalkCookieManager(); 33 mCookieManager.setAcceptCookie(false); 34 mCookieManager.setAcceptFileSchemeCookies(false); 35 36 //ProgressDialogを生成します。 37 progressDialog = new ProgressDialog(this); 38 progressDialog.setMessage("実行中です。"); 39 40 //ハンドラを生成 41 mHandler = new Handler(); 42 43 ((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View v) { 46 mXWalkView.reload(0); 47 } 48 }); 49 50 ((Button)findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View v) { 53 mXWalkView.stopLoading(); 54 } 55 }); 56 57 ((Button)findViewById(R.id.button3)).setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 for(int fa = 0; fa < 10; fa++) { 61 buttonProcess(); 62 } 63 } 64 }); 65 66 } 67 68 @Override 69 protected void onPause() { 70 super.onPause(); 71 if (mXWalkView != null) { 72 mXWalkView.pauseTimers(); 73 mXWalkView.onHide(); 74 } 75 } 76 77 @Override 78 protected void onResume() { 79 super.onResume(); 80 if (mXWalkView != null) { 81 mXWalkView.resumeTimers(); 82 mXWalkView.onShow(); 83 } 84 } 85 86 @Override 87 protected void onDestroy() { 88 super.onDestroy(); 89 if (mXWalkView != null) { 90 mXWalkView.onDestroy(); 91 } 92 } 93 94 95 //buttonがクリックされた時の処理 96 private void buttonProcess() { 97 //ProgressDialogを表示します。 98 progressDialog.show(); 99 //画面更新 100 mXWalkView.reload(0); 101 //スレッドを生成して起動します。 102 MyThread thread = new MyThread(); 103 thread.start(); 104 } 105 106 class MyThread extends Thread { 107 public void run() { 108 try { 109 Thread.sleep(100); 110 } catch (InterruptedException e) { 111 } 112 //メインスレッドのメッセージキューにメッセージを登録します。 113 mHandler.post(new Runnable() { 114 //run()の中の処理はメインスレッドで動作されます。 115 public void run() { 116 //画面更新中止 117 mXWalkView.stopLoading(); 118 //ProgressDialogを消去します。 119 progressDialog.dismiss(); 120 } 121 }); 122 try { 123 Thread.sleep(100); 124 } catch (InterruptedException e) { 125 } 126 } 127 } 128 129}
activity_main.xml
xml
1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="0dp" 4 android:paddingRight="0dp" 5 android:paddingTop="0dp" 6 android:paddingBottom="0dp" tools:context=".MainActivity"> 7 8 <!-- XWalkViewを追加 --> 9 <org.xwalk.core.XWalkView 10 android:id="@+id/xwalkWebView" 11 android:orientation="vertical" 12 android:layout_width="fill_parent" 13 android:layout_height="fill_parent" 14 android:background="#000000" 15 /> 16 17 <LinearLayout 18 android:id="@+id/linearLayout1" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" > 21 <Button 22 android:id="@+id/button1" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_weight="1" 26 android:text="更新" /> 27 <Button 28 android:id="@+id/button2" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_weight="1" 32 android:text="更新中止" /> 33 <Button 34 android:id="@+id/button3" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_weight="1" 38 android:text="連続更新中止" /> 39 <Button 40 android:id="@+id/button4" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:layout_weight="1" 44 android:text="none" /> 45 <Button 46 android:id="@+id/button5" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:layout_weight="1" 50 android:text="none" /> 51 </LinearLayout> 52 53</RelativeLayout>
AndroidManifest.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="パッケージ名"> 4 5 <uses-permission android:name="android.permission.INTERNET" /> 6 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 7 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 8 9 <application 10 android:allowBackup="true" 11 android:icon="@mipmap/ic_launcher" 12 android:label="@string/app_name" 13 android:supportsRtl="true" 14 android:theme="@style/AppTheme"> 15 <activity android:name=".MainActivity"> 16 <intent-filter> 17 <action android:name="android.intent.action.MAIN" /> 18 19 <category android:name="android.intent.category.LAUNCHER" /> 20 </intent-filter> 21 </activity> 22 </application> 23 24</manifest>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/21 04:42
2016/11/23 07:14 編集
2016/11/22 15:38
2016/11/23 07:14 編集
2016/11/23 08:23
2016/11/23 12:41