0

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);
    }
}

2 Answers2

0

I need a way of determining which particular class of the 5 classes called the IntentService, so the appropriate action is taken.

Put an extra on the Intent that you pass to startActivity() that indicates what the IntentService should do. You get a copy of that Intent in onHandleIntent() in the IntentService, so you can retrieve the extra and take appropriate steps based upon its value.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your reply. This is a good approach. However, I need a referece to the activity. Like its ContentResolver, and the state of some of its boolean variables. – Clinton Yeboah Mar 06 '16 at 21:14
  • This is one of the posts regarding send objects [How to pass an object from one activity to another on Android](http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android). I did start to do this but had problems with classes not being serializable so took another path (onResume worked for my situation). – MikeT Mar 06 '16 at 21:28
  • @ClintonYeboah: "However, I need a referece to the activity" -- that's not an appropriate option. "Like its ContentResolver" -- your `IntentService` is perfectly capable of getting its own `ContentResolver`, by calling `getContentResolver()`. "and the state of some of its boolean variables" -- pass those as extras as well. – CommonsWare Mar 06 '16 at 21:51
0

At the top of the activity I add:-

private final static String THIS_ACTIVITY = "AddProductToShopActivity";

for the intent I add:

intent.putExtra("CALLER", THIS_ACTIVITY);

In the started activity :-

final String caller = getIntent().getStringExtra("Caller");
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks for your reply. This is a good approach. However, I need a referece to the activity. Like its ContentResolver, and the state of some of its boolean variables. – Clinton Yeboah Mar 06 '16 at 21:13