Where The Streets Have No Name

Appspresso에서 activityListener, webViewClientListener 등록 본문

Developement/Mobile

Appspresso에서 activityListener, webViewClientListener 등록

highheat 2012. 4. 10. 10:53
package com.example.ax.echo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;

import com.appspresso.api.AxError;
import com.appspresso.api.AxPlugin;
import com.appspresso.api.AxPluginContext;
import com.appspresso.api.AxRuntimeContext;
import com.appspresso.api.activity.ActivityAdapter;
import com.appspresso.api.activity.ActivityListener;
import com.appspresso.api.view.WebViewClientAdapter;
import com.appspresso.api.view.WebViewClientListener;

public class MyPlugin implements AxPlugin {

	private AxRuntimeContext runtimeContext;
	private String TAG = "Echo";
	private ActivityListener activityListener;
	private WebViewClientListener webViewClientListener;

	@Override
	public void activate(AxRuntimeContext runtimeContext) {
		this.runtimeContext = runtimeContext;

		activityListener = new ActivityAdapter(){

			@Override
			public void onActivityPause(Activity activity) {
				Log.d(TAG,"onActivityPause");
				super.onActivityPause(activity);
			}

			@Override
			public void onActivityResume(Activity activity) {
				Log.d(TAG,"onActivityResume");
				super.onActivityResume(activity);
			}
			
		};
		runtimeContext.addActivityListener(activityListener);		
		
		webViewClientListener = new WebViewClientAdapter(){

			@Override
			public void onPageFinished(WebView view, String url) {
				Log.d(TAG,"onPageFinished");
				super.onPageFinished(view, url);
			}						
		};
		runtimeContext.addWebViewClientListener(webViewClientListener);
	}

	@Override
	public void deactivate(AxRuntimeContext runtimeContext) {
		this.runtimeContext = null;
		runtimeContext.removeActivityListener(activityListener);
		runtimeContext.removeWebViewClientListener(webViewClientListener);
	}

	@Override
	public void execute(AxPluginContext context) {
		String method = context.getMethod();

		if ("echo".equals(method)) {			
			String message = context.getParamAsString(0, null);
			context.sendResult(message);
			Intent smsIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:5551212;5551213"));
			smsIntent.putExtra("sms_body", "hello world");
			runtimeContext.getActivity().startActivity(smsIntent);
		}
		else {
			context.sendError(AxError.NOT_AVAILABLE_ERR);
		}
	}

}