মঙ্গলবার, ১২ মার্চ, ২০১৩

Object Oriented Concept

Inheritence is one of the most important feature of OOP.We know interface can inherit using implements keyword and we can not create an object of interface.
But Interface instance can hold a reference of any object and it is a interesting concept of OOP for design pattern.I want share simple topis using a simple java program

Step One :
Create a project and also create a Interface under the package of the project

public interface Ainterface {
 public void printSomething();

}
Step Two:
Create two class namely ClassA and ClassB as follow

public class ClassA implements Ainterface{
 @Override
 public void printSomething() {
  // TODO Auto-generated method stub
  System.out.print("Class A");
 }
 }


public class ClassB implements Ainterface {
 @Override
 public void printSomething() {
  // TODO Auto-generated method stub
  System.out.print("Class B");
 }

}

Step Three :
Create main class and run the project

public class Main {
 public static void main(String[] args) {
  Test(new ClassA());
  System.out.println();
  Test(new ClassB());

 }
 public static void Test(SamsungInterface s) {
  s.printSomething();
 }

}
Output:
ClassA
ClassB

রবিবার, ২৫ নভেম্বর, ২০১২

Integrate Android With C/C++

When i am working with Augmeted Reality for Android Device,i found a great implementation of C++ in java.Because C++ is best for some work like image processing,image detecting etc.However i suffered a lot when worked with AR because i have no knowledge about integration of C/C++ with java.
Now i am sharing this topic

Firstly We must set up our environment for do the work.As a android developer one must install following feature.
  1. JDK
  2. Eclipse IDE
  3. Android SDK Downloader
  4. Android ADT
  5. Android SDK platform support
But if we work with C/C++ we must set following two
  1. Cygwin Environment
  2. Android NDK
Note that set environment carefully because without proper installtion it's doesn't work.

Cygwin Environment  Setup:


For Cygwin Environment  Go to http://www.cygwin.com/setup.exe and select "Install from the Internet!" when prompted at "Choose A Download Source" in the installer. I recommend not changing the Root Directory in the next window, and leaving it at "C:\cygwin". The "Local Package Directory" holds the downloaded packages. You may want to keep them with the downloaded Setup.exe in the same directory so as to have a Cygwin installer directory. Choose a download site with a known fast connection near you.


When the package information is downloaded you will see a hierarchical browser to select packages. Select the following package from the hierarchy for download:

All -> Devel -> "make: The GNU version of the 'make' utility"

Select the word "skip" to change it to the actual version number, which is currently 3.81-2. Finish the installation by clicking next.

Your Cygwin environment is fully set-up to work with the avobe topic. Now we set  windows path variable to point to "C:\cygwin\bin". by Computer(Righ Click) -> Properties -> Advance System Setting ->  Environment Variable ->  Edit .Then Copy  "C:\cygwin\bin;"  and paste it.


NDK Setup:


The Android NDK is an extension to the Android SDK that lets Android developers build performance-critical parts of their applications in native code. Download the NDK package from

Unzip the archive and copy the contents into a directory. To be consistent with our your previous setup i recommend that you put the contents in the same drive where you put Android SDK and Eclipse. Thus Android SDK and Android NDK share the same parent directory.
Now set the Environment variable as the same as above with   path"C:\Development\Android\android-ndk-r7\"


Now our Environment is set for work.

  1. Lets start with creating a new project in eclipse.There is two important thing for next work and they are Activity Name and Package Name.
  2. We create a new folder in project root directory named jni.
  3. Create a file in jni folder with name hello.c
  4. Copy the following code and paste to it.
    #include<string.h>
    #include<jni.h>

    JNIEXPORT
    jstring JNICALL

    Java_com_integratingstuff_jni_Hello_getString
    (JNIEnv* env, jobject thiz){


    return (*env)->NewStringUTF(env, "String Come From C");
    }
  1. Now Look at the code.Red mark text indicate your package name and change it to your project package name.Green mark text indicate the Activity name and change it to your activity name and blue color text is function name that will be called later.Orange color text is the return type of this function.                                                                                                              
  2. Create another file in jni folder with name Android.mk.Copy the following code and paste to it.
    LOCAL_PATH := $(call my-dir)
    include

    $(CLEAR_VARS)

    # Here we give our module name and source file(s)
    LOCAL_MODULE := hello
    LOCAL_SRC_FILES := hello.c


    include

    $(BUILD_SHARED_LIBRARY)

  3. Create another file in jni folder with name Application.mk.Copy the following code and paste to it.
    APP_ABI := armeabi armeabi-v7a
  4. Open your layout from res folder and paste following code to it.
    <

    RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="" />

    </
    RelativeLayout>
  5. Now open your Activity.java file in src folder.Copy the following code and paste to it.
    package com.integratingstuff.jni;
    importa ndroid.os.Bundle;

    import android.app.Activity;

    import android.util.Log;
    import android.view.Menu ;
    import android.widget.TextView;
    import android.widget.Toast;
    public
    class Hello extends Activity {
    TextView t;
    public native String getString();

    static {
    System.loadLibrary(
    "hello-jni");
    }
  6. @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.
    activity_hello);
    String s = getString();
    Log.e("Text Asce-------------------", s);
    t = (TextView) findViewById(R.id.text1);
    t.setText(s);
    Toast.makeText(Hello.
    this, s, Toast.LENGTH_LONG).show();
    }

    }





    Now Run Cygwin.exe.And goto the directory where the project reside.If your project reside in "D:\Sample"
    then write following command on Cygwin window and execute

    cd D:/Sample/YOUR_PROJECT_NAME

    Then execute another command "ndk-build" on cygwin window.
    After that Build your project in eclipse and run it on device or emulator.
    Hope output will Come

















শনিবার, ৮ অক্টোবর, ২০১১

android মোবাইল এর মাধ্যমে mysql ডাটাবেস এ connection এবং ডাটা access


 এই Project এ আমরা একটি PHP Script ব্যবহার করব যেটা android phone কে mysql databaseএর সাথে connect করবে।

 


আমরা তিনটি ধাপে Project সম্পন্ন করতে পারি।ধাপগুলো নিম্নরুপঃ


ধাপ ১
       Project টি যখন রান করবে তখন একটি PHP Script Android Phone কে MySql Database এর সাথে    connect   করবে।
ধাপ ২
      PHP Script টি ডাটাবেস থেকে ডাটা আনবে এবং JSON(JavaScript Object Notation) format এ পরিবরতন করবে।তারপর সেই ডাটা পাঠিয়ে দিবে android Device এ।

ধাপ ৩
     Android Phone ডাটা গুলো পেলে সেটা parse করবে এবং phone এ দেখবে


প্রথমে আমরা একটি নতুন android project create করব‍.

Run Eclipse -> File -> New -> Android Project.

তারপর নিচের চিএের মত field গুলো পূরন করি

 click Finish বাটন. Project  টি  তৈরি হয়ে গেলে এটা রান করব।

যেহেতু আমরা local server ব্যবহার সেহেতু আমরা XAMPP setup করে নিব।XAMPP Control Panel রান করব এবং Apache আর mysql start করব।
তারপর আমরা c:/xampp/htdocs folder এ Test.php file টি রাখব।

Test.php code
<?php
mysql_connect("localhost","root","");
mysql_select_db("world");
$sql=mysql_query("select * from countryName where  country like 'B%'");
while($row=mysql_fetch_assoc($sql))
$output[ ]=$row;
print(json_encode($output));
mysql_close(); 
?>
 


আমরা localhost world নামে একটি ডাটাবেজ create করব।যেখানে countrrName নামে একটি table এবং table এ country নামে একটি field থাকবে যার datatype হবে varchar।Bangldesh data টি insert করি।



এরপর আমাদের project এর res/layout folder এ যাব এবং main.xml file টি open করব।
main.xml এর code টুকু মুছে ফেলি এবং নিচের code টুকু copy করি

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>

</LinearLayout>

এবার আমরা src\com.PL\PLActivity file টি অপেন করি(PL হল project name)।
package com.PL;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

import android.app.ListActivity;
public class PLActivity extends ListActivity {
    /** Called when the activity is first created. */
    JSONArray jArray;
    String result=null;
    InputStream is = null;
    StringBuilder sb=null;
      String cityName ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       final Button changeButton = (Button) findViewById(R.id.textColorButton);
       
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
   
      try{
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://10.0.2.2/Test.php");
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
           is = entity.getContent();
           }catch(Exception e){
               Log.e("log_tag", "Error in http connection"+e.toString());
          }
      //convert response to string
      try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
             sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");
             String line="0";
             while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
              }
              is.close();
              result=sb.toString();
              }
      catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
              }
      //paring data
   
      try{
            jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                   json_data = jArray.getJSONObject(i);
                   cityName =json_data.getString("countryName");
                  
                   Toast.makeText(getBaseContext(), "Country Foun Found" ,Toast.LENGTH_LONG).show();
                   changeButton.setText(cityName);
            }
            catch(JSONException e1){
                Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                  e1.printStackTrace();
          }
      }

    }


এবার project run করুন।আশা করি কাজ করবে।
যদি কোন problem হয় তাহলে mail করুন sajolsaha01494@gmail.com