Monday, January 2, 2012

Beginning Android Programming

Hello,

Here is my second blog on Android and this is some what different blog. Here I am going to share some useful tips for beginners of android. These are the issues which I faced as a beginner in Android.

1) How to setup your phone for testing an Application.

Normally android phones does not allow to test application but you can configure it. Go to Settings->Applications

There will be a check box here Unknown Sources (Allow installation of Non market applications). It must be checked in . If not it will only application installation from android market.

After that Go to Settings->Applications->Development. Here there will be another check box USB debugging (Debug mode when USB is connected) It must be checked in. If not, it will not catch an exception in LogCat of eclipse IDE.

2) How to uninstall application from your virtual device

There might be the case that while you are testing with virtual device, you need to re install application. For that you have to use Android ADB. Keep your virtual device on  while working through adb. That you can find in Android android-sdk\platform-tools Go to command prompt and navigate to above location. After that type following command. 
adb shell. 

It will open an adb shell. Go to data/app folder here you will find application apk file. Remove apk file using following command.
rm application_name.apk

This will remove application from virtual device. If you want to remove application from phone you can do it via Applications->Manage Applications.

3) How to prevent activity from restarting on orientation change

This might be one of the common issue every beginners faces. You have declared an activity in in your manifest file. While testing in phone when you change orientation, application restart. To prevent this you need to add following attribute to your activity declaration in your manifest file.

android:configChanges="orientation"

This will save application data and state on change of orientation and will restore it back.

4) How to start new activity

We normally call it navigation. You have a one screen in one activity. On some user actions you want to load another screen. For that first you need to declare all the activities in your manifest file. Following is the code to start new activity

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(i);

5) How to pass data between two activities 

It might be the case that you need to pass some parameters from one activity to another. Following is the code you need to add in intent of new activity.

Intent i = new Intent(CurrentActivity.this, NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString("param", "value");
i.putExtras(bundle);
startActivity(i);

In new activity you can access this parameter in onCreate method as follow.

Bundle bundle = this.getIntent().getExtras();
param = bundle.getString("param");


I hope this helps you.

Tuesday, December 27, 2011

Use existing Sqlite database in Android

Hello,

Since last few days, I have been working with android and I am very excited about it. This is my first blog on Android and many more is to come.

Normally Sqlite database is used with Android application. So this blog is about how you can use existing Sqlite database in Android application. First you need to add following table in your database. I recommend Sqlite  Expert Personal tool to make changes or create new Sqlite database. Open the database and run following query

"CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')"

After that insert record in it.

"INSERT INTO "android_metadata" VALUES ('en_US')"

Once done add your database file in assets folder of your android application. You can do simple copy paste here.

Now we will create class which will have all database related functions and methods. We will use SQLiteOpenHelper class provided by Android framework to connect with Sqlite database. Following is the class definition.

public class DatabaseAdapter extends SQLiteOpenHelper {
         private static String dbPath= "data/data/com.YourPackageName/applicationDb/";
         private static String dbName = "YourDBName";
         private SQLiteDatabase applicationDatabase; 
         private final Context applicationContext;

       
         public VocabTesterDatabaseHelper(Context context) {
                 super(context,  dbName , null, 3);
                 this. applicationContext  = context;
         }


         private boolean checkDataBase(){
                 File dbFile = new File( dbPath +  dbName);
return dbFile.exists();
  }


          private void copyDataBase() throws IOException{
try {

                  InputStream input =  applicationContext .getAssets().open(dbName);
                           String outPutFileName=  dbPath  +  dbName ;
                     OutputStream output = new FileOutputStream( outPutFileName);
                      byte[] buffer = new byte[1024];
                  int length;
                  while ((length = input.read(buffer))>0){
                 output.write(buffer, 0, length);
                  }
                  output.flush();
                  output.close();
                  input.close();
       }
                       catch (IOException e) {
                    Log.v("error",e.toString());
                    }
   }


             public void openDataBase() throws SQLException{
                String fullDbPath= dbPath + dbName;
             applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath,     null,SQLiteDatabase.OPEN_READONLY);
   }

                @Override
public synchronized void close() {
       if( applicationDatabase != null)
        applicationDatabase .close();
             super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}






}

That's it and your database helper class is ready. Initially we placed database in Assets folder and later on we moved it to particular folder in application. Still I am working on this class so I will add more functions to class and at the same time, I will upgrade this post. Stay tuned.

Thanks.




Tuesday, December 6, 2011

Case Study: Infusionsoft API Integration with Magento

Hello,

Here is another case study blog. My earlier case study blog was on ChannelAdvisor API  integration with Magento. You can read it here . Recently we were working on a Infusionsoft API integration with Magento.

What is Infusionsoft?

Infusionsoft helps small businesses in terms of marketing automationThey help small businesses to convert leads, grow sales and save time. They have CRMs which helps customer to centralize all their data at one location which can be easily accessible from anywhere. With their Email marketing customer can create emails which helps them in increasing sales and getting new clients. With their automation customer can create a system which can trigger behavior based communication, workflow  which helps them saving time and managing their sales and marking process.

Also they give nice set of REST APIs using which, it can be connected to ant third party applications and that was main requirement of our project.  Imagine the scenario that you have an Magento eCommerce site where you have thousands of customers registered and you are planning to use Infusionsoft as a primary service for your marketing.  In this case you need something between Magento and Infusionsoft  that can communicate user information.

We integrated Infusionsoft API with Magento registration. So that whenever a new user registers in Magento, their details automatically sent to Infusionsoft database using API. In case user updates information in Magento same  information is updated in Infusionsoft too. In case Admin deletes  user account, that particular user will be removed from Infustionsoft database  too.  This is the complete integration of Infusionsoft API with Magento.

This is very useful in case you have some user preferences in Magento database and based on that you want to send them marketing emails and in many other cases.