Question: Need assistance with how to write the following code for Android Studio: Create a SharedPreference that stores when the external data was last reloaded and

Need assistance with how to write the following code for Android Studio:

Create a SharedPreference that stores when the external data was last reloaded and then reload the external data only if the elapsed time is greater than the data download interval you want.

The app pulls data from a .JSON file and writes it to internal file. The question is asking to create a SharedPreference that stores when this external data was last written.

For context, here is my current code for the fragment where I am trying to create the shared Preference:

package edu.ucdenver.contactlistultimate; import android.app.Activity; import android.app.ListFragment; import android.content.SharedPreferences; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; import android.os.Bundle; import android.view.View; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import android.os.AsyncTask; /**  * A simple {@link ListFragment} subclass.  * Activities that contain this fragment must implement the  * {@link ItemFragment.OnListFragmentInteractionListener} interface  * to handle interaction events.  */ public class ItemFragment extends ListFragment { private OnListFragmentInteractionListener mListener; private static final String ARG_TITLE = "mytitle"; private String mTitle; //Generic string array containing list items  private String[] items = {"Item 1", "Item 2", "Item 3", "item 4"}; public static ItemFragment newInstance(String param1) { ItemFragment fragment = new ItemFragment(); Bundle args = new Bundle(); args.putString(ARG_TITLE, param1); fragment.setArguments(args); return fragment; } public ItemFragment() { // Required empty public constructor  } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(getArguments() != null) { mTitle = getArguments().getString(ARG_TITLE); getActivity().setTitle(mTitle); } // To do: Change Adapter to display your content   // setListAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1, items));   // setRetainInstance(true);   new HttpGetTask().execute(); } final SharedPreferences prefs = getActivity().getPreferences(getActivity().MODE_PRIVATE); ////Shared preference created here private class HttpGetTask extends AsyncTask { private static final String TAG = "HttpGetTask"; private static final String URL = "http://ucd.dawngregg.com/LeFantTyler_Contacts.js"; @Override protected List doInBackground(Void... params) { List data = null; HttpURLConnection httpUrlConnection = null; JSONResponseHandler mClient = new JSONResponseHandler(); try { httpUrlConnection = (HttpURLConnection) new URL(URL).openConnection(); InputStream in = new BufferedInputStream( httpUrlConnection.getInputStream()); String jdata = readStream(in); mClient.writeFile(getActivity(), jdata); data = mClient.handleResponse(jdata); } catch (MalformedURLException exception) { Log.e(TAG, "MalformedURLException"); } catch (IOException exception) { Log.e(TAG, "IOException"); } finally { if (null != httpUrlConnection) httpUrlConnection.disconnect(); } //Log.i(TAG,data);  return data; } protected void onPostExecute(List result) { // Create a ListAdapter from the JSON List data  setListAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, result)); } // private utility function to get all lines from the remote URL and add them to a single string.  private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer data = new StringBuffer(""); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { data.append(line); } } catch (IOException e) { Log.e(TAG, "IOException"); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return data.toString(); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnListFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnListFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); if (null != mListener) { mListener.onListFragmentInteraction("", position); } } public interface OnListFragmentInteractionListener { public void onListFragmentInteraction(String x, int y); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!