Showing posts from January, 2014

Threading and Asynchronous Processing In Android OS - Part 1

Some tasks in application development require threading and asynchronous processing due to complexity of the task. For example networking, complex calculations, querying data over the network, parsing data, accessing remote database require threading. Unless Android Operating system decide that application is not responding and throw Application Not Responding (ANR) event.

There are three ways to manage complex and time consuming processes. They are,
By using AsynTask helper class.By using standard Thread class.Or using the Loader class.AsyncTask Helper Class

Create subclass of AsyncTask and implement the appropriate callback method from below list.

onPreExecute() - This method runs on the UI thread before beginning of background process.doInBackground() - This method runs where real task is taken place. For example if you want to perform complex task, you can do inside this method.publishProgress() - This method called from the doInBackground() method. This will inform UI thread about …

[Android] Display Yes/No Alert box using java.

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Do this action"); builder.setMessage("do you want confirm this action?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do do my action here dialog.dismiss(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // I do not need any action here you might dialog.dismiss(); } }); AlertDialog alert = builder.create();;
In the following line, remember to specify your class.
AlertDialog.Builder builder = new AlertDialog.Builder(this);

For example it should change like this.
AlertDialog.Builder builder = new AlertDialog.Builder(ClassName.this);

[Android] How to restart service?

This is two lines of codes, I use to restart my Service class. First line will stop my service. Second line will start the service again.

stopService(newIntent(this,YourService.class)); startService(newIntent(this,YourService.class));