Android Calling Built In Application

In Android features, there is one of feature is Calling one application from another application smoothly and seamlessly. This feature allows you to integrate various applications on your device to get consistent relation experience between two applications.
This article covers, how can you call application using various ways on your device.
There are many ways to call applications, Its all depends on which application you are going to call. In this article you will learn calling that applications that are commonly available in your Android device.
Such as.
1. Calling phone dialing application
2. Displaying Google Map
3. Launching Google Play
4. Sending Email
5. Sharing Tex Message
6. Opening Image to In Image Viewer application

Calling phone dialing application

To call any specific number from your application via existing phone number dialing application presented in you device, you can launch that app using “tel:” scheme, see here.

When preceding code will run, you will get application launched with phone number which you passed by Intent from your application. See screenshot of phone dialing application. You may see different interface of dialing app on your device.
Calling phone dialing application

Displaying Google Map

To launch google map with specific location from your appliation, you can use “geo:” scheme. For example, you want to open Taj Mahal Location, then just pass location data “geo:27.175076,78.042464” of Taj Mahal. See the code illustrated below.

When the code will run, you may see the map chooser dialog with more or different apps. Like Earth, Maps, Browser etc.
Displaying Google Map
Just choose Maps application from chooser. And you the Google Map will open with location which you passed via Intent using scheme.
Displaying Google Map

Launching Google Play

Some time we may required to redirect the user on Google Play (Google Market) to get another application. In that case you can use “market:” scheme.

The above code will launch Google Play Store and it will display the Number Puzzle app.

Launching Google Play

Sending Email

To send an email from your application. use the following code.

When above code will run, the email program chooser you may see.

Sending Email

For example choose Gmail program

Sending Email

Sharing Tex Message

Some time you may want to send or share a message/content to another app. Such as Facebook or Messaging application. For it you can use following code.

Application chooser will display like below image
Sharing Tex Message

For example choose Messaging application

Sharing Tex Message

Opening Image to In Image Viewer application

If you want to launch an image viewing application to view image from your application, use following code snippet.

You may find chooser with Image viewing applications list.
Opening Image to In Image Viewer application
Opening Image to In Image Viewer application

Download Source

Finding Second Largest Number From Array in JavaScript

While working on one of the client project, I just wrote a functions to find 1st and 2nd Largest Numbers in JavaScript. And I would like to share with you one of them which demonstrates how to find second largest number using javascript.

OOP Principles: Introduction

OOP…!, When this topic comes while learning any of the programming language which is object oriented, some time may or may not, but we feel little pressure on mind & think about ‘Can I work with oop?’ or ‘Is the oop easy or difficult to understand?’ like so….
And my answer is OOP (Object-Oriented Programing) is really great think in programming to learn and also easy to understand.
When I was started learn programming, I was created my first program in Lingo. Yes absolutely correct – In Macromedia Director 6. And that time I was completely unknown about programming concepts & OOP principles.
After few months I aware about oops little bit with confused mind. but handling time to time various type of programs using oop terms, I got the good experience.
With the OOP terminology you can design the architecture of your applications or programs. It helps you to design your applications programming structure & flow. You can define the programming patterns as a library using OOP and you can use it many applications. You can make your classes reusable. Classes is one of the main thing in OOP.
If you want to learn OOP principles, learn this principles one by one and step by step with small example of code.
Look at the below,
Package – This thing allows you to organize your classes by separate groups.
Classes – Class is the collections of methods(functions) and you can make any number of instance of that. Changing its property(Variables) or passing variables by calling methods you can take different outputs from it.
Composition – Allows you to make the multiple instances of same classes. If class ‘Bike’ has a class ‘Wheel’, then relation between this classes is composition,
Inheritance – Allows you to extend new class from an existing class with all the properties and methods without doing copy paste the entire code. If class ‘Bike’ is a ‘Vehicle’, then relation between this classes is inheritance.
Polymorphism – Using the same method name in different classes and defining multiple states with single property, can allows to apply based on type or specification. You don’t need to change code every time you use different objects or components.
Encapsulation – This thing take the data & code safe or in other words we can say hiding & protecting data of an object or class.

Here I’ve discussed OOP principles in short. If you have any comment on this post. Please welcome.

Passing Variables Between Flash ActionScript and PHP

In the most of the web based flash applications we required to communicate with server side scripting languages like PHP to send data to database and receive data from database. It is because flash don’t have support to directly read data from database.
In this article I am going to show you , how to pass variables or data between Flash and PHP.
Prerequisites:
To call php url you need web server. You may use any of the hosting which support php script or you can setup web server on your local pc also. Find the documentation on this link to setup WAMP. http://www.wampserver.com/en/

Passing Variable from Flash to PHP

To pass variables from Flash to PHP first we have to define URLRequest and URLLoader to load URLRequest. See below.
[as3]
var url:String = “http://ajaybadgujar.com/uploads/2013/11/passvars/username.php”;
_request = new URLRequest(url);
_urlLoader = new URLLoader();
[/as3]
Now define URLVariables object and set all the variables which you want to send to PHP using GET method. To know more about GET and POST Methods, visit http://www.php.net/
[as3]
var _vars:URLVariables = new URLVariables();
_vars.id = 1;
_request.data = _vars;
_request.method = URLRequestMethod.GET;
[/as3]
Now load the request and add Event.COMPLETE and IOErrorEvent.IO_ERROR event listeners to URLLoader object.
[as3]
_urlLoader.load(_request);
_urlLoader.addEventListener(Event.COMPLETE, onComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
[/as3]
The complete url will look like this with variables.
“http://ajaybadgujar.com/uploads/2013/11/passvars/username.php?id=1”

GETing passed data from flash to PHP

See the code below, how to get data and pass the values to flash using url variables.

Receive data from PHP to Flash

Inside the onComplate(e:Event) function receive result data.
[as3]
private static function onComplete(e:Event):void {
_urlLoader.removeEventListener(Event.COMPLETE, onComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
vars = new URLVariables(e.target.data);
trace(“Name : ” + vars.fname + ” ” + vars.lname);
}
[/as3]

Complete Source

See the class Illustrated below named “VARLoader”.
[as3]
package com.ajaybadgujar.dataloader{
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLVariables;
public class VARLoader {

public static var onSuccess:Function = null;
public static var onFailure:Function = null;

public static var _request:URLRequest = null;
public static var _urlLoader:URLLoader = null;

public static function loadData(_fileName:String, _vars:URLVariables, _onSuccess:Function, _onFailure:Function):void{

onSuccess = _onSuccess;
onFailure = _onFailure;

trace(‘Data load prepairing’);
var url:String = _fileName;
_request = new URLRequest(url);
_urlLoader = new URLLoader();
_request.data = _vars;

_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

_urlLoader.load(_request);
_urlLoader.addEventListener(Event.COMPLETE, onComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);

}

private static function onComplete(e:Event):void {
_urlLoader.removeEventListener(Event.COMPLETE, onComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
onSuccess(new URLVariables(e.target.data));
trace(“Data loading success”);
}

private static function onError(e:IOErrorEvent):void{
trace(‘Connection Error’);
onFailure();
}

}

}
[/as3]

How to use

[as3]
package{
import flash.display.Sprite;
import flash.net.URLVariables;
import com.ajaybadgujar.dataloader.VARLoader;

public class Main extends Sprite{

var url:String = “http://ajaybadgujar.com/uploads/2013/11/passvars/username.php”;

public function Main() {
var vars:URLVariables = new URLVariables();
vars.id = 1;
VARLoader.loadData(url, vars, DisplayName, LoadingNameFail);
}

private function DisplayName(vars:URLVariables):void{
trace(“Name : ” + vars.fname + ” ” + vars.lname);
}

private function LoadingNameFail():void{
trace(“Loading name failed”);
}

}

}
[/as3]

Result:

Passing Variables Between Flash ActionScript and PHP

Android Auto Launch Application at Boot Time

You may noticed, on your device some of the applications launched automatically while your device boot completely. Like, your email app or any chat application.
But how it works…?
In this article, I am going to show you, how we can auto launch application at boot time. So, let’s see.
Create new class named “AutoLaunch.java” and extend it by “BroadcastReceiver” as base class.
Note: You must register BroadcastReceiver under AndroidManifest.xml.
See the code Illustrated below.

Now when your device boot, it will fire broadcast receiver and call onReceiver() method of the class. Inside the onReceiver() method, use the Intent object to start another Activity which you want to launch at first Or you may use some think else which you want.
Note: You must add the FLAG _ACTIVITY_NEW_TASK flag to the Intent object.
Now register the broadcast receiver under application node into AndroidManifest.xml. Also add RECEIVE_BOOT_COMPLETED permission.
See the receiver node added into AndroidManifest.xml below.

Your application is ready to run.

Android Auto Launch Application at Boot Time

Download Source

How to create Android Contact Book App : Part 7 : Deleting Contact Details from SQLite Database

In this part, we will cover how to delete contact details from SQLite Contact Database.
Android Deleting Contact Details from SQLite Database
Open “ContactHandler.java” file from previous part and add following function to delete contact details by contact id.

Open “ContactDetails.java” file and define ContactHandler named “handler”.

Now add the following code snippet under “case 2” into “onOptionsItemSelected ()” function.

Complete “ContactDetails.java”

I hope, you like this tutorial with 7 parts.
Download the final source of the Tutorial.

Loading XML into Flash using ActionScript 3

You may seen many of the Data Driven web and AIR applications developed using Flash ActionScript or Flex.
Here I am going to show you how we can load XML Data into Flash using ActionScript to make data driven flash or AIR application.
Create a class named “XMLLoader” and save the file with the same name of class.
[as3]
package com.ajaybadgujar.dataloader{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;

public class XMLLoader{

public static var onSuccess:Function = null;
public static var onFailure:Function = null;

public static var _request:URLRequest = null;
public static var _urlLoader:URLLoader = null;

public static function loadXML(_fileName:String, _onSuccess:Function, _onFailure:Function):void{

onSuccess = _onSuccess;
onFailure = _onFailure;
trace(‘Data load prepairing’);
var url:String = _fileName;
_request = new URLRequest(url);
_urlLoader = new URLLoader();
trace(“Loading XML…..”);
_urlLoader.load(_request);
_urlLoader.addEventListener(Event.COMPLETE, onComplete);
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);

}

private static function onComplete(e:Event):void {
_urlLoader.removeEventListener(Event.COMPLETE, onComplete);
_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
onSuccess(XML(e.target.data));
trace(“Loading XML success”);
}

private static function onError(e:IOErrorEvent):void{
trace(‘Connection Error’);
onFailure();
}

}
}
[/as3]
Here is the sample “books.xml”.

Now see the below code, how you can load above XML by using XMLLoader class.
[as3]
package{
import flash.display.Sprite;
import com.ajaybadgujar.dataloader.XMLLoader;

public class Main extends Sprite{

private var filename:String = “books.xml”;

public function Main() {
// Load XML
XMLLoader.loadXML(filename, TraceXML, XMLLoadingFail);
}

public function TraceXML(_xml:XML):void{
trace(“Result: \n” + _xml);
}

public function XMLLoadingFail():void{
trace(“Loading XML Failed”);
}

}

}
[/as3]
And here is the result
loading_xml_into_flash_using_actionscript

Download Source

How to create Android Contact Book App : Part 6 : Updating Contact Details into Database

In this part you will learn, how to update contact details into database.


Table of Contents

  1. Creating Option Menu
  2. Passing Contact data to Edit Contact Activity
  3. Setting up Edit Contact Activity
  4. Updating Contact Details to Database

Creating Option Menu

Open existing “ContactDetails.java” file from the Part 5 Source files. And add following code to create Option Menu.

device-2013-10-27-172451

Passing Contact data to Edit Contact Activity

After creating Option Menu we have to add Item Select event. And Pass the data to Edit Activity for Editing Data. See code below.

Setting up Edit Contact Activity

Create a new Activity named “EditContact.java”. And add it into AndroidManifest.xml under Appliction Node.

Now setup the Layout to Activity. For Edit Contact we can use same layout as we used previously for NewContact Activity. And load all the passed data come from last activity.

Updating Contact Details to Database

Now get all the data from Input fields and ImageView and set it into new Contact Object.

After setting into object Pass Object to database handler

Function into ContactHandler Class

And load Main Activity once data get updated into database.

snap

Creating Chess Board at Runtime using ActionScript

I experienced many time while developing games, some time we may required to create the graphics at runtime. Here I am going to share with you a same kind of Chess Board example, which is created at runtime using ActionScript.

Here you can move black piece on white boxes and white piece on black boxes.

Download Source

Creating Box Class

Create a new Flash file with main.as Document class file.
Now let’s start with creating Box class and name it “box.as”.
[as3]
/*box.as*/
package{
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.Shape;

// Defining class as dynamic class so it allows to assign/create new properties from other class
public dynamic class box extends Sprite{
protected var boxColor:Number;
protected var boxWidth:Number;
protected var boxHeight:Number;

public function box(_boxColor:uint, _boxWidth:Number, _boxHeight:Number){
super();

boxColor = _boxColor;
boxWidth = _boxWidth;
boxHeight = _boxHeight;

init();
}

protected function init():void{
// creating box
var s:Shape = new Shape();
s.graphics.beginFill(boxColor);
s.graphics.drawRect(0,0,boxWidth,boxHeight);
s.graphics.endFill();
this.addChild(s);
}
}
}
[/as3]

Creating Chess Board

In the main.as first we will define some Variables and Constants types.
[as3]
protected const ROWS:int = 8;
protected const COLS:int = 8;
protected const BOX_WIDTH:int = 40;
protected const BOX_HEIGHT:int = 40;
protected const BOX_COLOR_WHITE:uint = 0xFFFFFF;
protected const BOX_COLOR_BLACK:uint = 0x000000;
protected const SNAP_DISTANCE:int = 15;

protected var boxes_list:Array = new Array(); // for all boxes
protected var boxes_white_list:Array = new Array(); // for white boxes only
protected var boxes_black_list:Array = new Array(); // for black boxes only
[/as3]

Now place all the boxes to stage.
[as3]
protected function placeBoxes():void{
// placing 64 boxes
var b:box;
var yCord:int = 40;
//var remender:int = 0;
var row:int = 1;

for(var i:int=0; iPlacing Pieces

Now here we have to place some pieces to play.
[as3]
protected function placeKings():void{
// placing white king to intial position
king_white = new king_w();
king_white.x = boxes_black_list[30].x;
king_white.y = boxes_black_list[30].y;
king_white.mcColor = ‘white’;
addChild(king_white);

// placing black king to intial position
king_black = new king_b();
king_black.x = boxes_white_list[2].x;
king_black.y = boxes_white_list[2].y;
king_black.mcColor = ‘black’;
addChild(king_black);
}
[/as3]

Making Pieces Interactive

And finally assign interaction to pieces
[as3]
protected function assingInteractivity():void{
king_black.addEventListener(MouseEvent.MOUSE_DOWN, fnStartDrag);
king_black.addEventListener(MouseEvent.MOUSE_UP, fnStopDrag);

king_white.addEventListener(MouseEvent.MOUSE_DOWN, fnStartDrag);
king_white.addEventListener(MouseEvent.MOUSE_UP, fnStopDrag);
}

protected function fnStartDrag(e:MouseEvent):void{
e.currentTarget.lastX = e.currentTarget.x;
e.currentTarget.lastY = e.currentTarget.y;
e.currentTarget.startDrag(false,rect);
e.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, fnStop);
}

protected function fnStopDrag(e:MouseEvent):void{
e.currentTarget.stopDrag();
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, fnStop);
// checking overlaping with boxes

var isSnap:Boolean = false;

for(var i:int=0; i= distance(e.currentTarget,boxes_list[i])){

e.currentTarget.x = boxes_list[i].x;
e.currentTarget.y = boxes_list[i].y;

isSnap = true;

break;
}
}

if(isSnap != true){
e.currentTarget.x = e.currentTarget.lastX;
e.currentTarget.y = e.currentTarget.lastY;
}
}

protected function fnStop(e:MouseEvent):void{
e.currentTarget.stopDrag();
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, fnStop);
e.currentTarget.x = e.currentTarget.lastX;
e.currentTarget.y = e.currentTarget.lastY;
}

protected function distance(mc1:Object,mc2:Object):Number{
var dist = Math.sqrt((mc1.y-mc2.y)*(mc1.y-mc2.y) + (mc1.x-mc2.x)*(mc1.x-mc2.x));
return dist;
}
[/as3]

Complete main.as

[as3]
/*main.as*/
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

public class main extends Sprite{
protected const ROWS:int = 8;
protected const COLS:int = 8;
protected const BOX_WIDTH:int = 40;
protected const BOX_HEIGHT:int = 40;
protected const BOX_COLOR_WHITE:uint = 0xFFFFFF;
protected const BOX_COLOR_BLACK:uint = 0x000000;
protected const SNAP_DISTANCE:int = 15;

protected var boxes_list:Array = new Array(); // for all boxes
protected var boxes_white_list:Array = new Array(); // for white boxes only
protected var boxes_black_list:Array = new Array(); // for black boxes only

protected var king_white:king_w;
protected var king_black:king_b;

protected var rect:Rectangle = new Rectangle(115,40,280,280);

// Constructor
public function main():void{
super();
init();
}

protected function init():void{
placeBoxes(); // placing chess boxes
placeKings(); // placing kings to respective positions
assingInteractivity();
}

protected function placeBoxes():void{
// placing 64 boxes
var b:box;
var yCord:int = 40;
//var remender:int = 0;
var row:int = 1;

for(var i:int=0; i= distance(e.currentTarget,boxes_list[i])){

e.currentTarget.x = boxes_list[i].x;
e.currentTarget.y = boxes_list[i].y;

isSnap = true;

break;
}
}

if(isSnap != true){
e.currentTarget.x = e.currentTarget.lastX;
e.currentTarget.y = e.currentTarget.lastY;
}
}

protected function fnStop(e:MouseEvent):void{
e.currentTarget.stopDrag();
e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, fnStop);
e.currentTarget.x = e.currentTarget.lastX;
e.currentTarget.y = e.currentTarget.lastY;
}

protected function distance(mc1:Object,mc2:Object):Number{
var dist = Math.sqrt((mc1.y-mc2.y)*(mc1.y-mc2.y) + (mc1.x-mc2.x)*(mc1.x-mc2.x));
return dist;
}
}
}
[/as3]

Creating Chess Board at Runtime using ActionScript

Android Passing Object Between Activities

In the preceding article I have shown, How to Pass Data Between Activities. In this article I am going to cover how we can Pass an Object between Activities. See the screenshots of an example below.

Android Passing Object Between Activities

Android Passing Object Between Activities

Download Source from here.

In addition passing the data types using putExtra() and putExtras() method, We can also pass the Object using Intent object.
Let’s see how.
Suppose you have your own custom class. For example A Song class. Make sure it should be implements as a Serializable class. See below example of custom Song class.

Now use the putExtra() method to pass an Object to another Activity.

Use the getSerializableExtra() method of Intent Object to retrieve passed object to targeted activity by key you set. And typecast the result to the custom class you created.

See the code below.