Sunday, March 18, 2012

Saving Objects(Synchronized) in Files in Android


We can save the all the synchronized Objects in Android using File Connections even if the Object is a customized or user defined object.

 Saving the Objects:


step 1: 

Open a file using the below code.

File suspend_f = new File("/data/data/com.rama.rama/cache/check.txt");

here i had saved the file in the application space in the device which will be removed when the application is un-installed. You can save it any where. but you should remember it for further fetching of the objects.

Step 2:

Opening an outputStream for the File object and writing the object to file as shown below.

                fos = new FileOutputStream(suspend_f);
                oos = new ObjectOutputStream(fos);
                oos.writeInt(10);
                for(int i=0 ;i<10;i++)
                oos.writeObject(new Lecture("Mahesh"+i, "sta", "end", "one", true));

We should read the objects in the same order that you write objects.


Reading the Objects:


step 1:


Opening the file object just as like the above step1.

Step 2:


Opening InputStream of the File Object and reading the objects in the same order that we write in the file.


                fis = new FileInputStream(suspend_f);
                 ois = new ObjectInputStream(fis);
                 String s = "";
                 int j=ois.readInt();
                 for(int i=0;i                 {
                _l = (Lecture)ois.readObject();
                s+=_l;
                 }
The whole code can be downloaded from this link.
Blog Update
Custom Broadcasts:

Unlike system originated broadcasts, these are the broadcasts which are sent by user. Usually these are useful in communicating back with activity from services.

Step 1: Write the below code where ever you want to send a custom broadcast:

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.vl.dumptruck.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);

In above code, we are creating a Intent with custom action and broadcasting it.

Step 2: Write the below code in the class which will be responsible for receiving broadcasts:
private
BroadcastReceiver logoutReceiver;
IntentFilter logoutIntentFilter = new IntentFilter();
logoutIntentFilter.addAction("com.vl.dumptruck.ACTION_LOGOUT");
logoutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             //perform the task here
        }
};
registerReceiver(logoutReceiver, logoutIntentFilter);

1. In the above code, we are creating an IntentFilter with the action same as specified when sending the broadcast.
2. And a BroadcastReceiver, which is responsible for performing a task upon receing the broadcast(override the onReceive() method).
3. Now, register the broadcast using registerReceiver() method by passing BroadcastReceiver and IntentFilter objects. Here receiver is to call the callback method onReceive() and intent filter is to check whether the the receiver is authorized to respond to the broadcasted intent.
4. Finally, unregister the broadcast receiver using unregisterReceiver() by passing the receiver object, otherwise you will get a broadcast leaked exception.

        unregisterReceiver(logoutReceiver);

Note: Be careful with registering and unregistering broadcasts.
1. If you register a broadcast in onCreate(), then you have to unregister it in onDestroy(), with this the activity is capable of receiving the broadcasts even when activity is in background(i.e, in onPause() state also).
2. If you register a broadcast in onCreate() and you dont want to respond to broadcasts when you are in background(onPause()), then unregister your receiver in onPause() and register again in onResume() to start receiving broadcasts again and finally unregister it in onDestroy().



Move Android Apps Installation to SD Card
Please start using the tag name  android:installation as shown below to enable the option  to move the installation files to SDCard.
           package="xxx.xx.xxxx"
          android:versionCode="1"
           android:versionName="0.9.6" android:installLocation= "auto" >
This property is introduced in Introduced in  API Level 8.

Sunday, January 29, 2012

Android Project Tracking & Management & Tools

BlogUpdater_1_27_2012.doc
        Hi friends, after a long time I want to help all of you people in the below concerns.
  1. Architecture:
  2. Exception Tracking
  3. 2DScrollView
  4. C2DM
  5. Tools
Architecture
        I know lot of you people knows about this. But, let me explain this for the other people. The structure of the project should be as shown in thebelow format.
In the above image the custom Activity is the Super Activity for all of the activities where you can add unique requirements throughout the project. i.e., Exception tracking/MenuItems handling according to currentModule etc…,
First of all we should take care about the architecture while we are starting a project which will helps you a lot in the process of project enhancements and re-usability.
package.ui: Here we need to add all the custom views that are developed. So that we can use these UI from anywhere in the project and can also be maintained same for all of your future projects.
package.util: Here we need to add all the constants and utility functions like validations for dates/times or project specific things which can be used from the project.
Dispatchmodule: This is the startup module for all the projects. So that you can easily identify the starting activity in all the projects.
        Every module should have an engine. All the activities should implement CustomActivity which is there in the package.ui which is implementing Activity.
Module1,2…..n:  Depending upon the project, the number of modules will be divided to manage the project with ease.
At any instant, we can know all the information about the project (i.e., Module ID, Activity ID, Stack which contains all the Activity instances which are running in the background etc…,).
NetworkHandler: We should write all the handlers which can give the project running status information. Every time we starting a module/activity, this class should be intimated with the corresponding information.
Exception Tracking:
         Here is a simple class file which will be very useful to track your Android applications even after sending it to the client or to the market or to the testers. This class will always track your application and it will get informed with all the exceptions (even if it is a Force Close).
How to use this:
  1. Just copy and paste the downloaded java file in to your project.
  2. Create an instance for this activity in the CustomActivity as shown below.
protected PostMortemReportExceptionHandler mDamageReport = new PostMortemReportExceptionHandler(this);

 
  1. Just to start the exception tracking.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);                                                            mDamageReport.initialize();
}
  1. Closing/Stopping the tracker in the Activity.
protected void onDestroy() {
                                               mDamageReport.restoreOriginalHandler();
                                               mDamageReport = null;
                                               super.onDestroy();
                                    }
        Every thing done. You can get updated with crash or exceptions.
Customizing the Tracker:
                We can customize the way of getting the information of the exception or crash from the mobile device to the server. Either we can go for creating a specific web service to catch the crash report or else we can go for mail option in which we can respect the user’s privacy and can ask for his permission to send the mail.
private static final int SENDING_OPTION = 1;// 0 --> Send Through Mail (Need user interference)
                                                                      // 1 --> Sending without user interference or confirmation
By using this variable, we can select the options wether we can directly post it to our webservice or to take the user's permission by preparing a mail and asking him to send it. 
if we want to send it to a mail id the we can specify the mail id for the below variable
private static final String MSG_SENDTO = xyz@gmail.com;
private static final String BUG_TRACKER_URL = http://tracking.webservice/gather.php

// This is the webservice URL which is used to post the issue info
2DscrollView:
In Android, we should not add a scrollview to another scrollview.  Because, If we add in such  a way then it will leads to the memory issues. If you want both horizontal and vertical scrolls for a single view, then you should go for a CustomFrameView.
For further information about the 2Dscroll view please click on the below link.


C2DM:
Cloud To Device Messaging is a concept which is introduced in Android from OS version 2.2.  The concept of C2DM is to optimize the network usage of the Android applications in push notifications.
Normally for every application, in order to get the push notifications, we should have a separate thread /service running in the background of the operating system continuously hitting the server in equal intervals of time which will drains the battery of the device and unnecessary usage of network. If we want push notifications for ‘n’ number of applications, then we should run ‘n’ number of services to get the notifications for n applications.
For every Android device, there will be default Google service running in the background which will take care about all the Google applications in device. After 2.2, they gave us an option to use the same Google service. So, that there is no need of running separate threads for each and every application.
We should send the desired message with the device ID to the Google C2DM server. then, the message will be sent to the device from the Google C2DM server.
We can get the device ID from the device after registering it to the Google C2DM server. We should send this ID from device to our Application third party Server. Then after, the application server will send the message to that specific device by using this device ID.
The device ID will be updated every time when the Google server get refreshed. Then the Google server will automatically send a broadcast message to the device and then we should get the device ID again and should intimate about the change of the Device ID to application third party server. So, that the application server will send the messages by using the updated device ID.
I will update this later by adding the code snippets later.
TOOLS:
DDMS:
This is used to fetch the logs, process id’s  of the applications and to get the memory usage of the operating system and many more.
ADB:
This is used to install/uninstall/to fetch running devices/pushing or pulling files/opening shell/fetching logs/ starting or killing the adb server.
ADT:
 This is a plugin for eclipse, which will be helpful in developing android applications in Eclipse.
9-patch:
This tool is used to optimize the usage of assets. This tool will add a single line pixel around the image. In which we can specify whether the corresponding pixels can be stretched or not. If we change the white space to black, then it resembles that the corresponding pixels are stretchable.
Hierarchyviewer:
This is used to view the hierarchy of the layouts in the application. Even we can download the psd’d of the screen which will provide the complete information of the screen in photoshop.
Monkeyrunner:
 This is a tool for automation testing. We can specify the number of turns for testing basing on which it will test the application.
sqlite3:
 This is used to check the sqllite database.