Sunday, March 16, 2014

Android Tutorial : Passing object by Intent using Parcelable

According to google engineers, this code will run significantly faster. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
However, it is obvious here that implementing Parcelable is not free. There is a significant amount of boilerplate code and it makes the classes harder to read and maintain.
package  com.cocoalibrary.objectPass;  
 import android.os.Parcel;  
 import android.os.Parcelable;  
 public class Library implements Parcelable {  
     private String bookName;  
     private String author;  
     private int publishTime;  

     public String getBookName() {  
  return bookName;  
     }  
     public void setBookName(String bookName) {  
  this.bookName = bookName;  
     }  
     public String getAuthor() {  
  return author;  
     }  
     public void setAuthor(String author) {  
  this.author = author;  
     }  
     public int getPublishTime() {  
  return publishTime;  
     }  
     public void setPublishTime(int publishTime) {  
  this.publishTime = publishTime;  
     }  

     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {  
  public Book createFromParcel(Parcel source) {  
      Library mBook = new Library();  
      mBook.bookName = source.readString();  
      mBook.author = source.readString();  
      mBook.publishTime = source.readInt();  
      return mBook;  
  }  
  public Book[] newArray(int size) {  
      return new Book[size];  
  }  
     };  

     public int describeContents() {  
  return 0;  
     }  
     public void writeToParcel(Parcel parcel, int flags) {  
  parcel.writeString(bookName);  
  parcel.writeString(author);  
  parcel.writeInt(publishTime);  
     }  
 }

package com.cocoalibrary.objectPass;  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
public class ParcelableDemo extends Activity implements OnClickListener {  

    private Button button;  
     
    public  final static String PAR_KEY = "com.cocoalibrary.parcelableKey";  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);     
        setupViews();  

    }  

    public void setupViews(){  
       
        button = (Button)findViewById(R.id.button);  
        
        button.setOnClickListener(this);  
    }  
 

    public void PacelableMethod(){  
        Library mBook = new Library();  
        mBook.setBookName("Pacelable Tutorial");  
        mBook.setAuthor("Raja");  
        mBook.setPublishTime(2014);  
        Intent mIntent = new Intent(this, PacelableDemo.class);  
        Bundle mBundle = new Bundle();  
        mBundle.putParcelable(PAR_KEY, mBook);  
        mIntent.putExtras(mBundle);  

        startActivity(mIntent);  
    }  

    public void onClick(View v) {   
            PacelableMethod();  
          
    }  
}

package com.cocoaLibrary.objectPass;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class DetailDataView extends Activity {  

     public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  TextView mTextView = new TextView(this);  
  Book mBook = (Book)getIntent().getParcelableExtra(PacelableDemo.PAR_KEY);  
  mTextView.setText("Library name is: " + mBook.getBookName()+"/n"+  
      "Author is: " + mBook.getAuthor() + "/n" +  
      "PublishTime is: " + mBook.getPublishTime());  
  setContentView(mTextView);  
     }  
 }

If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources.
However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it  but  remember that serialization is an expensive operation so keep it to a minimum.
If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.





Friday, March 14, 2014

JSON Object Simple Example Read And Write by Android

1. Write JSON to file

In below example, it write JSON data via JSONObject and JSONArray, and save it into a file named “sample.json“.
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
 
public class JsonSimpleExample {
     public static void main(String[] args) {
 
 JSONObject obj = new JSONObject();
 obj.put("name", "cocoalibrary.com");
 obj.put("age", new Integer(50));
 
 JSONArray list = new JSONArray();
 list.add("value 1");
 list.add("value 2");
 list.add("value 3");
 
 obj.put("messages", list);
 
 try {
 
  FileWriter file = new FileWriter("c:\\sample.json");
  file.write(obj.toJSONString());
  file.flush();
  file.close();
 
 } catch (IOException e) {
  e.printStackTrace();
 }
 
 System.out.print(obj);
 
     }
 
}
Output – See content of file named “sample.json“.
{
 "age":100,
 "name":"cocoalibrary.com",
 "messages":["Name 1","Name 2","Name 3"]
}

3. Read JSON from file

Use JSONParser to read above generated JSON file “simple.json“, and display each of the values.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.cocoalibrary.JSONArray;
import org.json.cocoalibrary.JSONObject;
import org.json.cocoalibrary.parser.JSONParser;
import org.json.cocoalibrary.parser.ParseException;
 
public class JsonExample {
     public static void main(String[] args) {
 
 JSONParser parser = new JSONParser();
 
 try {
 
  Object obj = parser.parse(new FileReader("c:\\sample.json"));
 
  JSONObject jsonObject = (JSONObject) obj;
 
  String name = (String) jsonObject.get("name");
  System.out.println(name);
 
  long age = (Long) jsonObject.get("age");
  System.out.println(age);
 
  // loop array
  JSONArray msg = (JSONArray) jsonObject.get("messages");
  Iterator<String> iterator = msg.iterator();
  while (iterator.hasNext()) {
   System.out.println(iterator.next());
  }
 
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (ParseException e) {
  e.printStackTrace();
 }
 
     }
 
}
Output
cocoalibrary.com
100
Name 1
Name 2
Name 3

Getting Android source code from an APK file

I'll show you other way to decompile the .apk files.
Don't waste your valuable time for that one. you can visit below link very easy and less time for re-engineering for apk to source code.
http://www.decompileandroid.com/