Archive

Archive for May, 2012

Make a phone call from Android app

Pass phone number to ‘call’ function, android will make a call

 
private void call(String number) {
		try {
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:" + number));
			_context.startActivity(callIntent);
		} catch (ActivityNotFoundException e) {
			Log.e("TAG", "Call failed", e);
		}
	}
Advertisements
Categories: Android Tags:

Send email from android app

private void sendMail(String mailID) {
	Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailID, null));
        _context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Categories: Android Tags:

Call a function in android using string value(Using Java Reflection)

public void reflectionSample(String functionName){
		///this is used to call java methods using string value.		    
		Class c = MyClassName.class;
		///paraTypes: used to pass parameter to java function, its just a declaration.
		Class paraTypes[] = new Class[1];
		///the type of parameter is hash map.
		paraTypes[0] = HashMap.class;
		Method method = null;
		try {
			///class.getDeclaredMethod(java-function-name, java-function-parameter-type)
			///@param1: the string name of java function in class c
			///@param2: the type of parameter, like hashmap, string, int, etc
			method = c.getDeclaredMethod(functionName, paraTypes);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> argument = new HashMap<String, String>();
		argument.put("functionName", functionName);

		Object arg[] = new Object[1];
		arg[0] = new HashMap(argument);
		try {
			///method.invoke calls the java function
			method.invoke(this, arg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void calledFromReflection(HashMap<String, String> text){
		System.out.println("---------------|"+text.get("functionName")+"|-------------");
	}


reflectionSample("calledFromReflection");

Learn about reflection
Reflection doc

Categories: Android Tags: ,

Android Hybrid app

                ///webview loads the given url.
		mainWebView.loadUrl("file:///android_asset/mainView.html");
		///to enable javascript functions.
		mainWebView.getSettings().setJavaScriptEnabled(true);
		///open android's function to javascript.
		mainWebView.addJavascriptInterface(this, "DemoActivity");
		///this is used remove two or five pixed gap on right side on view.
		mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

		///to listen javascript's alert function.
		mainWebView.setWebChromeClient(new WebChromeClient(){
			@Override
			public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
				return super.onJsAlert(view, url, message, result);
			}
		});

		mainWebView.setWebViewClient(new WebViewClient(){
			
			///listens all external urls.
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
			}

			///is called when url not found or any other error while loading the page.
			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				Toast.makeText(_context, description, Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				//This is used to ignore ssl error(it will not check ssl certificate and proceeds).
				handler.proceed();
			}

		});

		//to bring focus on web view
		//this enable keyboard on screen 
		mainWebView.requestFocus(View.FOCUS_DOWN);
		mainWebView.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_UP:
					if (!v.hasFocus()) {
						v.requestFocus();
					}
					break;
				}
				return false;
			}
		});
///to call javascript function.
mainWebView.loadUrl("javascript:changeValue('"+value+"')");

Javascript file

function showMenu(){
	DemoActivity.showMenu();
}

function hideMenu(name){
	DemoActivity.hideMenu(name);
}

function changeValue(name){
	document.getElementById("messageView").innerHTML = name;
}

You can download the source code from
http://dl.dropbox.com/u/12044966/Demos.zip

Categories: Android Tags:
%d bloggers like this: