Posts

Showing posts from March, 2013

How to Stop AsyncTask in Android

AsyncTask is used for Threading Appliction. In most situation we want to stop in when its in running state. For example when we downloading or uploading file in the middle of process we want to stop the use the following code. MyAsyncTask objUploader; if (objUploader != null && objUploader.getStatus() != AsyncTask.Status.FINISHED) objUploader.cancel(true); Use the following code in the AsynchTask class method doInBackground() if (isCancelled()) { break; }

Toggle Android Airplan mode

This code show to Toggle Airplane mode of mobile public void airplaneModeOn( ) {         try {             boolean isEnabled =  Settings.System.getInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;             Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, isEnabled    ? 0 : 1);             Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);             intent.putExtra("state", !isEnabled);             sendBroadcast(intent);             Toast.makeText(this, "Mode is "+!isEnabled  , Toast.LENGTH_LONG).show();         } catch (Exception e) {             Toast.makeText(this, "exception:" + e.toString(), Toast.LENGTH_LONG).show();         }    } Called this method from button click event to Toggle Airplane mode.