Pages

Tuesday 29 October 2013

Screenshot Using DDMS on Windows(Android Device)


What is DDMS?

Android provides a debugging tool called the Dalvik Debug Monitor Server (DDMS). 

Advantages of DDMS:

   1 . DDMS provides port-forwarding services.
    2 . Screen capture on the device.
    3 . Thread and heap information on the device.
    4 . Logcat.
    5 . Process.
    6 . Radio state information.
    7  . Incoming call and SMS spoofing, location data spoofing, and more.

Screen Shot

1. First Create AVD, open   Emulator .
2open DDMS Prospective ,DDMS Windows With the List of Devices Connected to it.




3  Finally Click on Save Button.



Saturday 26 October 2013

Handler and AsyncTask in Android


Android Handler

  1. Handler allows to add messages to the thread which creates it and It also enables you to schedule some runnable to execute at some time in future.
  2. The Handler is associated with the application’s main thread. It handles and schedules messages and runnables sent from background threads to the app main thread.
  3. If you are doing multiple repeated tasks, for example downloading multiple images which are to be displayed in ImageViews (like downloading thumbnails) upon download, use a task queue with Handler.
  4. There are two main uses for a Handler. First is to schedule messages and runnables to be executed as some point in the future; and second Is to enqueue an action to be performed on a different thread than your own.
  5. Scheduling messages is accomplished with the the methods like post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods.
  6. When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create.
  7. You can create your own threads, and communicate back with the main application thread through a Handler.

Android AsynkTask

  1. Async task enables you to implement MultiThreading without get Hands dirty into threads. AsyncTask enables proper and easy use of the UI thread. It allows performing background operations and passing the results on the UI thread.
  2. If you are doing something isolated related to UI, for example downloading data to present in a list, go ahead and use AsyncTask.
  3. AsyncTasks should ideally be used for short operations (a few seconds at the most.)
  4. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
  5. In onPreExecute you can define code, which need to be executed before background processing starts.
  6. doInBackground have code which needs to be executed in background, here in doInBackground we can send results to multiple times to event thread by publishProgress() method, to notify background processing has been completed we can return results simply.
  7. onProgressUpdate() method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update event thread
  8. onPostExecute() method handles results returned by doInBackground method.
  9. The generic types used are
    • Params, the type of the parameters sent to the task upon execution
    • Progress, the type of the progress units published during the background computation.
    • Result, the type of the result of the background computation.
  10. If an async task not using any types, then it can be marked as Void type.
  11. An running async task can be cancelled by calling cancel(boolean) method.

Friday 25 October 2013

AsyncroTask Example To Get Server Data


AsyncroTask Basics:

     1.  AsyncTask provide easy way to use of the UI thread.
 
     2.  Perform background operations and publish results on the UI thread without having to     manipulate             threads and/or handlers.

     3.  AsyncTask is designed to be a helper class around Thread and Handler and does not use a generic              threading framework.

     4.  AsyncTasks should ideally be used for short operations (a few seconds at the most.)

     5.  The AsyncTask class must be loaded on the UI thread.

     6. The task instance must be created on the UI thread.

     7.   execute(Params...)  must be invoked on the UI thread.


 Usage:

                         Taking same example as we have done in previous example Thread With Handlers - Android Example  In this example  we are creating a thread and call http GET method to get server response and after got the response,then do other functionality ( Save Data in database or show alert ,Redirect to another activity).


Program Code  :

  Asyncronoustask.class

public class Asyncronoustask extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);
        
         
        final Button GetServerData = (Button) findViewById(R.id.btuserver);
        
        GetServerData.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String serverURL = "http://www.mobile-tech.in/"; 
new LongOperation().execute(serverURL);
}
        });
        
    }
    
    private class LongOperation  extends AsyncTask<String, Void, Void> {
   
        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(Asyncronoustask.this);
        TextView uiUpdate = (TextView) findViewById(R.id.output);
        protected void onPreExecute() {
       
        uiUpdate.setText("Output : ");
            Dialog.setMessage("Downloading source..");
            Dialog.show();
        }

        protected Void doInBackground(String... urls) {
            try {
           
           
      HttpGet httpget = new HttpGet(urls[0]);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);
                
            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }
            
            return null;
        }
        
        protected void onPostExecute(Void unused) {
       
            Dialog.dismiss();
            
            if (Error != null) {
                
                uiUpdate.setText("Output : "+Error);
                
            } else {
           
            uiUpdate.setText("Output : "+Content);
           
             }
        }
        
    }
}


Main.xml  :

 <Button 
        android:paddingTop="10px"
        android:id="@+id/btuserver" 
    android:text="Get Data Server " 
    android:cursorVisible="true"
    android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"  
android:layout_gravity="center_horizontal"
    /> 
    
    <TextView
        android:paddingTop="20px"
        android:id="@+id/output"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Data Will Display here" />



Out Put :

1.       


2 .    


3 .      


Sunday 6 October 2013


Using Intent Flags


When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity(). The flags you can use to modify the default behavior are:





FLAG_ACTIVITY_NEW_TASK

                     Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in   onNewIntent().
This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.


FLAG_ACTIVITY_SINGLE_TOP

             If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to onNewIntent(), instead of creating a new instance of the activity.
This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.


FLAG_ACTIVITY_CLEAR_TOP

            If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).
There is no value for the launchMode attribute that produces this behavior.

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.