I am working on a news app. I have 5 categories of news sections. Each tab or section, calls to a different URL and has a separate table in the local database, hence a different URI(using a ContentProvider), when retrieving local data.
I have one AsyncTask that services all the requests. It determines which url or uri to call based on the instance of the class passed to it. All the tabs/sections/fragments/classes inherit from a common base class..
Now, I will like to change the AsyncTask to an IntentService, so I can use the AlarmManager class. I have noticed that, there seems to be no easy way of passing an object via intents.
I need a way of determining which particular class of the 5 classes called the IntentService, so the appropriate action is taken.
This class is called when the instance of the class is to be determined:
public class GetURL {
public static URL GetURL(TabsSuperClass tabs)
{
URL url = null;
try {
if(tabs instanceof CultureFrag)
{
final String baseUri = "http://content.guardianapis.com/search?";
Uri uriBuilder = Uri.parse(baseUri)
.buildUpon()
.appendQueryParameter("section", "culture|local|music|books|society")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("use-date", "published")
.appendQueryParameter("show-fields", "trailText,thumbnail")
.appendQueryParameter("page", String.valueOf(TabsSuperClass.pageSize))
.appendQueryParameter("page-size", "10")
.appendQueryParameter("api-key", "Test-Key")
.build();
url = new URL(uriBuilder.toString());
}
else if(tabs instanceof LifeStyleFrag)
{
final String baseUri = "http://content.guardianapis.com/search?";
Uri uriBuilder = Uri.parse(baseUri)
.buildUpon()
.appendQueryParameter("section", "lifeandstyle|education|fashion|help")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("use-date", "published")
.appendQueryParameter("show-fields", "trailText,thumbnail")
.appendQueryParameter("page", String.valueOf(LifeStyleFrag.pageSize))
.appendQueryParameter("page-size", "10")
.appendQueryParameter("api-key", "Test-Key")
.build();
url = new URL(uriBuilder.toString());
}
else if(tabs instanceof ScienceFrag)
{
final String baseUri = "http://content.guardianapis.com/search?";
Uri uriBuilder = Uri.parse(baseUri)
.buildUpon()
.appendQueryParameter("section", "science|environment|technology|business")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("use-date", "published")
.appendQueryParameter("show-fields", "trailText,thumbnail")
.appendQueryParameter("page", String.valueOf(ScienceFrag.pageSize))
.appendQueryParameter("page-size", "10")
.appendQueryParameter("api-key", "Test-Key")
.build();
url = new URL(uriBuilder.toString());
}
else if(tabs instanceof SportFrag)
{
final String baseUri = "http://content.guardianapis.com/search?";
Uri uriBuilder = Uri.parse(baseUri)
.buildUpon()
.appendQueryParameter("section", "sport|football")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("use-date", "published")
.appendQueryParameter("show-fields", "trailText,thumbnail")
.appendQueryParameter("page", String.valueOf(SportFrag.pageSize))
.appendQueryParameter("page-size", "10")
.appendQueryParameter("api-key", "Test-Key")
.build();
url = new URL(uriBuilder.toString());
}
else if(tabs instanceof WorldFrag)
{
final String baseUri = "http://content.guardianapis.com/search?";
Uri uriBuilder = Uri.parse(baseUri)
.buildUpon()
.appendQueryParameter("section", "world|opinion|media|us-news|australia-news|uk-news")
.appendQueryParameter("order-by", "newest")
.appendQueryParameter("use-date", "published")
.appendQueryParameter("show-fields", "trailText,thumbnail")
.appendQueryParameter("page", String.valueOf(WorldFrag.pageSize))
.appendQueryParameter("page-size", "10")
.appendQueryParameter("api-key", "Test-Key")
.build();
url = new URL(uriBuilder.toString());
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
return url;
}
public static Uri GetContentUri(TabsSuperClass tabs)
{
Uri uri = null;
if(tabs instanceof CultureFrag)
{
return NewsContract.CONTENT_URI_CULTURE;
}
else if(tabs instanceof LifeStyleFrag)
{
return NewsContract.CONTENT_URI_LIFESTYLE;
}
else if(tabs instanceof ScienceFrag)
{
return NewsContract.CONTENT_URI_SCIENCE;
}
else if(tabs instanceof SportFrag)
{
return NewsContract.CONTENT_URI_SPORT;
}
else if(tabs instanceof WorldFrag)
{
return NewsContract.CONTENT_URI_WORLD;
}
return uri;
}
}
urlConnection = (HttpURLConnection)GetURL.GetURL(_fragment).openConnection();
An instance where this class is called is here, when I am inserting data into the table of the class which called
private void InsertIntoTable(List<NewsFacade> data) {
for (NewsFacade facade :
data) {
ContentValues values = new ContentValues();
values.put(NewsContract.DataContract.COLUMN_NAME_DATE, facade.getDate());
values.put(NewsContract.DataContract.COLUMN_NAME_CONTENT, facade.getText());
values.put(NewsContract.DataContract.COLUMN_NAME_TAG, facade.getTag());
byte[] image = EncodeImage(facade.getThumb());
values.put(NewsContract.DataContract.COLUMN_NAME_THUMB, image);
values.put(NewsContract.DataContract.COLUMN_NAME_TITLE, facade.getTitle());
values.put(NewsContract.DataContract.COLUMN_NAME_WEBADDRESS, facade.getWebAddress());
_fragment.mResolver.insert(GetURL.GetContentUri(_fragment), values);
}
}