确定哪个类称为IntentService

时间:2016-03-06 21:01:47

标签: java android android-asynctask

我正在制作新闻应用。我有5类新闻栏目。每个选项卡或部分调用不同的URL并在本地数据库中具有单独的表,因此在检索本地数据时使用不同的URI(使用ContentProvider)。

我有一个AsyncTask为所有请求提供服务。它根据传递给它的类的实例确定要调用哪个url或uri。所有选项卡/部分/片段/类都从公共基类继承。

现在,我想将AsyncTask更改为IntentService,因此我可以使用AlarmManager类。我注意到,似乎没有简单的方法通过意图传递对象。

我需要一种方法来确定5个类中哪个特定类称为IntentService,因此采取了相应的操作。

当要确定类的实例时调用此类:

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

这个类被调用的实例在这里,当我将数据插入到调用类的表中时

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 个答案:

答案 0 :(得分:0)

  

我需要一种方法来确定5个类中哪个特定类称为IntentService,因此采取了相应的操作。

在您传递给Intent的{​​{1}}上额外加上startActivity(),表明IntentService应该做什么。您会在Intent的{​​{1}}中获得onHandleIntent()的{​​{1}}副本,因此您可以检索额外内容并根据其值采取适当的步骤。

答案 1 :(得分:0)

在活动的顶部,我添加: -

private final static String THIS_ACTIVITY = "AddProductToShopActivity";

我添加的意图:

intent.putExtra("CALLER", THIS_ACTIVITY);

在已开始的活动中: -

final String caller = getIntent().getStringExtra("Caller");