facebook twitter hatena line email

「Android/カスタムListener」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==独自Listenerを作る方法== 足し算処理を例にする -CustomData.java public class CustomData { public int code; public String message; public C...」)
 
行98: 行98:
 
  test: onFinish()
 
  test: onFinish()
 
  test: onClose()
 
  test: onClose()
 +
 +
==Listenerをメインスレッドで処理しない場合==
 +
-MainActivityの以下を書き換える
 +
  -      CustomTask task = new CustomTask();
 +
  +      CustomTask task = new CustomTask(this);
 +
 +
-CustomTask.java
 +
public class CustomTask {
 +
    private Activity mActivity;
 +
    private CustomListener mListener;
 +
    private CustomData data;
 +
    CustomTask(Activity activity) {
 +
        mActivity = activity;
 +
        data = new CustomData(123, "hello");
 +
    }
 +
    public void exec(int num, int addnum, int sum) {
 +
        this.mActivity.runOnUiThread(new Runnable() {
 +
            public void run() {
 +
                if (CustomTask.this.mListener != null) {
 +
                    CustomTask.this.mListener.onLoad(data);
 +
                    CustomTask.this.mListener.onStart(data);
 +
                }
 +
            }
 +
        });
 +
        if (num + addnum == sum) {
 +
            data.message = "success " + num + "+" + addnum + "=" + sum;
 +
            this.mActivity.runOnUiThread(new Runnable() {
 +
                public void run() {
 +
                    if (CustomTask.this.mListener != null) {
 +
                        mListener.onSuccess(data);
 +
                    }
 +
                }
 +
            });
 +
        } else {
 +
            data.message = "error " + num + "+" + addnum + "!=" + sum;
 +
            this.mActivity.runOnUiThread(new Runnable() {
 +
                public void run() {
 +
                    if (CustomTask.this.mListener != null) {
 +
                        mListener.onError(data);
 +
                    }
 +
                }
 +
            });
 +
        }
 +
        this.mActivity.runOnUiThread(new Runnable() {
 +
            public void run() {
 +
                if (CustomTask.this.mListener != null) {
 +
                    mListener.onFinish(data);
 +
                    mListener.onClose(data);
 +
                }
 +
            }
 +
        });
 +
    }
 +
    public void setCustomListener(CustomListener listener) {
 +
        mListener = listener;
 +
    }
 +
}

2016年11月16日 (水) 14:20時点における版

独自Listenerを作る方法

足し算処理を例にする

-CustomData.java

public class CustomData {
   public int code;
   public String message;
   public CustomData(int code, String message) {
       this.code = code;
       this.message = message;
   }
}

-CustomListener.java

public interface CustomListener {
   void onLoad(CustomData data);
   void onStart(CustomData data);
   void onSuccess(CustomData data);
   void onError(CustomData data);
   void onFinish(CustomData data);
   void onClose(CustomData data);
}

-CustomTask.java

public class CustomTask {
   private CustomListener mListener;
   private CustomData data;
   CustomTask() {
       data = new CustomData(123, "hello");
   }
   public void exec(int num, int addnum, int sum) {
       mListener.onLoad(data);
       mListener.onStart(data);
       if (num + addnum == sum) {
           data.message = "success " + num + "+" + addnum + "=" + sum;
           mListener.onSuccess(data);
       } else {
           data.message = "error " + num + "+" + addnum + "!=" + sum;
           mListener.onError(data);
       }
       mListener.onFinish(data);
       mListener.onClose(data);
   }
   public void setCustomListener(CustomListener listener) {
       mListener = listener;
   }
}

-メイン処理(MainActivity.java)

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomListener listener = new CustomListener() {
            @Override
            public void onLoad(CustomData data) {
                Log.i("test", "task onLoad()");
            }
            @Override
            public void onStart(CustomData data) {
                Log.i("test", "task onStart()");
            }
            @Override
            public void onSuccess(CustomData data) {
                Log.i("test", "task onSuccess() code=" + data.code + " " + data.message);
            }
            @Override
            public void onError(CustomData data) {
                Log.i("test", "task onError() code=" + data.code + " " + data.message);
            }
            @Override
            public void onFinish(CustomData data) {
                Log.i("test", "task onFinish()");
            }
            @Override
            public void onClose(CustomData data) {
                Log.i("test", "task onClose()");
            }
        };
        CustomTask task = new CustomTask();
        task.setCustomListener(listener);
        task.exec(1, 1, 2); // 処理1
        task.exec(1, 1, 3); // 処理2
    }
}

処理1

test: onLoad()
test: onStart()
test: onSuccess() code=123 success 1+1=2
test: onFinish()
test: onClose()

処理2

test: onLoad()
test: onStart()
test: onError() code=123 error 1+1!=3
test: onFinish()
test: onClose()

Listenerをメインスレッドで処理しない場合

-MainActivityの以下を書き換える

 -      CustomTask task = new CustomTask();
 +      CustomTask task = new CustomTask(this);

-CustomTask.java

public class CustomTask {
   private Activity mActivity;
   private CustomListener mListener;
   private CustomData data;
   CustomTask(Activity activity) {
       mActivity = activity;
       data = new CustomData(123, "hello");
   }
   public void exec(int num, int addnum, int sum) {
       this.mActivity.runOnUiThread(new Runnable() {
           public void run() {
               if (CustomTask.this.mListener != null) {
                   CustomTask.this.mListener.onLoad(data);
                   CustomTask.this.mListener.onStart(data);
               }
           }
       });
       if (num + addnum == sum) {
           data.message = "success " + num + "+" + addnum + "=" + sum;
           this.mActivity.runOnUiThread(new Runnable() {
               public void run() {
                   if (CustomTask.this.mListener != null) {
                       mListener.onSuccess(data);
                   }
               }
           });
       } else {
           data.message = "error " + num + "+" + addnum + "!=" + sum;
           this.mActivity.runOnUiThread(new Runnable() {
               public void run() {
                   if (CustomTask.this.mListener != null) {
                       mListener.onError(data);
                   }
               }
           });
       }
       this.mActivity.runOnUiThread(new Runnable() {
           public void run() {
               if (CustomTask.this.mListener != null) {
                   mListener.onFinish(data);
                   mListener.onClose(data);
               }
           }
       });
   }
   public void setCustomListener(CustomListener listener) {
       mListener = listener;
   }
}