Friday, July 1, 2011

One difference between implementing Runnable and extending Thread is that by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same Object instance. for example.


public class Main {

   public static void main(String[] args) {
     RunnableThread r = new RunnableThread();
     Thread t1 = new Thread(r, "Thread A");
     Thread t2 = new Thread(r, "Thread B");
     ExtendsThread t3 = new ExtendsThread("Thread C");
     ExtendsThread t4 = new ExtendsThread("Thread D");
     t1.start();
     t2.start();
     t3.start();
     t4.start();

   }
 }



class RunnableThread implements Runnable {

private int counter;

  public void run() {
   try {
     for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName()+ ":"counter++);
      Thread.sleep(1000);
      }
    } catch (InterruptedException ine) {
    System.err.println(ine);
   }
 }
}

class ExtendsThread extends Thread {
  private int counter;
  ExtendsThread(String name) {
  super(name);
 }

 @Override
 public void run() {
  try {
   for (int i = 0; i != 2; i++) {
   System.out.println(Thread.currentThread().getName() + ": "+ counter++);
   Thread.sleep(1000);
   }
  } catch (InterruptedException ine) {
   System.err.println(ine);
 }
 }
}

Thursday, June 30, 2011

9 ways to improve your programming skills.

1. Learn a new programming language.
2. Read a good, challenging programming book.
Like: The Art of Computer Programming
Structure and interpretation of computer Programs.
A Discipline of Programming
3. Join an Open Source Project.
You Can join: GitHub, SourceForge,gitorius, bitBucket, Ohloh
4. Solve Programming puzzles.
5. Program Start writing a program. scratch. design all of the architechture and implement.
6. Read And Study Code.
7. Hang Out as programming sites and read blogs.
8. Write about coding.
9. learn low level programming like C, Assembly language.

JUnit Test Case with Example.

Here is Sample example on Junit Text Case.

Let's Test Some Code

public class Main {

public static void main(String[] args) {
}
static public int add(int a, int b) {
return a + b;
}

}


now i want to test about class using Junit Test case. fot test we require to import "junit.framework.*" and extend TestCase

import junit.framework.*;
public class TestMaths extends TestCase{

public void testMaths() {
int num1 = 2;
int num2 = 3;
int total = 5;
int sum = 0;
sum = Main.add(num1, num2);
assertEquals(sum, total);
}
}


Here i am using Netbeans. now run TestMaths file you can see out put on console.

Summary:
That's it! This example may be help full to getting start JUnit.

enjoy!

Tuesday, June 21, 2011

Android Flip View Example

Here i try to explain Android Flip View. Flip View is use to flip one view to another with use android set of animation.

Create Following Activity FlipeActivity.java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

public class FlipeActivity extends Activity {
ViewFlipper flipper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.details);
flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
Button btn=(Button)findViewById(R.id.flip_me);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
flipper.showNext();
}
});

}


}

we load here Animation xml file so we create first "anim" folder and create following file with set.
push_left_in.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:fromXDelta="100%p" android:toXDelta="15" android:duration="500"/>


push_left_out.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/>

main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

you can find more animation set here with example..
http://code.google.com/p/android-helloworld-samples/source/browse/trunk/ApiDemos/res/anim/

Monday, June 20, 2011

Android Services Example

Create Following Class for Services.

import android.app.Service;
import android.content.Intent;

import android.os.IBinder;
import android.util.Log;


public class MyServices extends Service{
private static final String TAG = MyServices.class.getSimpleName();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"MyServices Created");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG,"MyServices Started");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"MyServices Destroyd");
}

}

Register Services in AndroidManifest.xml file.

service android:name=".MyServices"


Call(Start) Services in Activity.

startService(new Intent(this,MyServices.class));

Blackberry Contact Event Handling.

Blackberry Contact Event Handling.

import java.util.Enumeration;
import java.util.Enumeration;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.PIMList;
import net.rim.blackberry.api.pdap.PIMListListener2;

final class MyPIMListener implements PIMListListener2 {
public void itemAdded(PIMItem item) {
if (item == null) {
return;
}
}

public void itemRemoved(PIMItem item) {
if (item == null) {
return;
}
}

public void itemUpdated(PIMItem oldItem, PIMItem newItem) {
if (oldItem == null || newItem == null) {
return;
}
itemRemoved(oldItem);
itemAdded(newItem);
}

public void batchOperation(PIMList list) {
if (list == null) {
return;
}
try {
ContactList contactList = (ContactList) list;
Enumeration e = contactList.items();
while (e.hasMoreElements()) {
Contact contact = (Contact) e.nextElement();
// ...
}
} catch (Exception e) {
// ...
}
}
}

Wednesday, June 15, 2011

Start android application on when Device start Or Boot Startup

Create MyBootStartUpListener.java in.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyBootStartUpListener extends BroadcastReceiver{
private static final String TAG = MyBootStartUpListener.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "My BootStartup");
try{
Intent homeIntent = new Intent(context,bootStartupActivity.class);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIntent);
}catch(Exception e){
System.err.println(e);
}
Log.d(TAG, "My BootStartup succefully");
}

}



receiver android:name=".MyBootStartUpListener"
intent-filter
action android:name="android.intent.action.BOOT_COMPLETED"
category android:name="android.intent.category.HOME"
intent-filter
receiver