Friday, 10 March 2017

Reusable AsyncTask

HI All!

In every android application as a developer, It may chance to call the AsyncTask for each and every execution of call the web-service.

To reduce the code, i have find way to reuse the AsyncTask, just we can create a common class for Asyctask and with the help of Interface can reuse at any n of time.



Business class for carry the web-service output & service name

/** * Created by selvaraj on 3/10/2017. */
public class Callable_Async_B {

// result is the json output as string// action_4 is the action to call (like register,login & etc..,)
String result, action_4;
public Callable_Async_B() { } public Callable_Async_B(String result, String action_4) { this.result = result; this.action_4 = action_4; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getAction_4() { return action_4; } public void setAction_4(String action_4) { this.action_4 = action_4; } }




Interface for call around the application
/** * Created by selvaraj on 3/10/2017. */
public interface AsyncResponse {
    void ProcessFinished(Callable_Async_B j_obj);
}












Common & Reusable Asynctask class
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/** * Created by selvaraj on 3/10/2017. */
public class MainAsyncObj extends AsyncTask<String, String, String> {

// Dialog to show while executing
ProgressDialog p_dialog;
// result & action_4 we know already// text_2_show is a value where the text can appears while execution of web-service 
String rersult_s, text_2_show, action_4;
// HashMap to carry al your parameter name & value 
HashMap<String, String> param_map;
// delegate is used to assign the Activity 
public AsyncResponse delegate = null;
// Activity to show on
Activity activity;
public MainAsyncObj(HashMap<String, String> param_map_, String text_2_show_, String action_4_, Activity activity_) { this.activity = activity_; this.text_2_show = text_2_show_; this.action_4 = action_4_; this.param_map = param_map_; } @Override protected void onPreExecute() { super.onPreExecute(); p_dialog = new ProgressDialog(activity); p_dialog.setMessage(text_2_show); p_dialog.setCancelable(false); p_dialog.setIndeterminate(false); p_dialog.show(); } @Override protected String doInBackground(String... strings) { JSONParser parser = new JSONParser(); List<NameValuePair> n_pair = new ArrayList<>(); for (String key : param_map.keySet()) { String value = param_map.get(key); n_pair.add(new BasicNameValuePair(key, value)); } rersult_s = parser.GenrtHttpRequest("http://000.000.00.000/" + action_4, "POST", n_pair); Log.d("Callable_result", rersult_s); return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); p_dialog.dismiss(); delegate.ProcessFinished(new Callable_Async_B(rersult_s, action_4)); } }





Reuse the Asynctask on Any Activity wherever you want 
// Implemet the interfacet to get the result 
public class Profile_F extends AppCompatActivity implements AsyncResponse{

// initiate the AsyncTask with parameters
HashMap<String, String> param_map = new HashMap<>();
// pass your Key Name & Value as you want
param_map.put("KEY_NAME", "VALUE");
param_map.put("KEY_NAME", "VALUE");
.... n param_map.put("KEY_NAME", "VALUE");
MainAsyncObj MAO = new MainAsyncObj(param_map,"Please Wait..,","register", this); MAO.delegate = this; MAO.execute();

// Result acter complete the AsyncTask
@Overridepublic void ProcessFinished(Callable_Async_B j_obj) {
    Log.d("Result",j_obj.getResult());
    Log.d("Action_4",j_obj.getAction_4());
}

Please Leave Your Comment..,

 


Enjoy The Day!
Have A Great Day!
Thank You..,


Android LKN

Hi!

By using Device we can track the user by their lastknown loc (LKN).

Method To Call The LKN

private Location getLastBestLocation() {

// get the locationmanager
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// get the status of GPS
    Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

// get the status of Provider
    Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    long GPSLocationTime = 0;
    if (null != locationGPS) {
        GPSLocationTime = locationGPS.getTime();
    }

    long NetLocationTime = 0;

    if (null != locationNet) {
        NetLocationTime = locationNet.getTime();
    }

    if (0 < GPSLocationTime - NetLocationTime) {
        return locationGPS;
    } else {
        return locationNet;
    }
}

// Convert the LKL as ADS

Location location = getLastBestLocation();
if (location != null) {
    latu = location.getLatitude();
    lngu = location.getLongitude();

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(PO_Entry.this, Locale.getDefault());

    try {
        addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getLocality();
        String state = addresses.get(0).getAdminArea();
        String country = addresses.get(0).getCountryName();
        String postalCode = addresses.get(0).getPostalCode();
        String knownName = addresses.get(0).getFeatureName();

        et_po_enty_loc.setText(address + " , " + city + "\n" + state + " , " + country + " , " + postalCode);
    } catch (IOException e) {
        e.printStackTrace();
    }
} else {
    et_po_enty_loc.setText("No Location Found");
}

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,