Context.startService(intent) 或 startService(intent)

4

Context.startService(intent)startService(intent)有什么区别?使用哪个有影响吗?

3个回答

7

只有一个startService()方法。 startService()Context类的方法,可用于ActivityServiceContext子类。


为什么有些人使用Context.startService(intent)而不是只使用startService(intent)? - mnish
你需要问他们。这并不是必要的。 - CommonsWare
请看我下面的答案以了解我们在编程中何时使用上下文 @mnish。 - Zar E Ahmer

2

正如Commonsware所说,只有一个startService()方法,即Context.startService(intent)

您的主活动程序本身就是Context的一个实例,因此不需要使用Context显式调用该方法(startService)。

这就像在类内部调用类的方法一样。


0

解释

Android中,每个人都知道如何使用Adapter。我们可以为它们创建单独的类。这将使我们的编码更简单易懂。但是当我们单独创建这些类时,它们需要一个上下文代表调用)。因此,在这种情况下,我们将活动的上下文传递给它们的构造函数。这样,Android就知道我们是代表哪个活动进行调用。

我们不能调用

getSystemService(Context...)//blah bhal

在单独的适配器类中,但我们可以在适配器构造函数中传递上下文并像这样调用它。

context.getSystemService(Context....)//

像这样调用您的适配器

ArticleAdapter adapter = new ArticleAdapter(context, list);

                    list_of_article.setAdapter(adapter);

并且像这样获取上下文...

ArticleAdapter.class

public class ArticleAdapter extends BaseAdapter
{
    Context context;

    ArrayList<HashMap<String, String>>  list;

    LayoutInflater inflater;

    public ArticleAdapter(Context context,
            ArrayList<HashMap<String, String>> list)
    {
        this.context = context;

        this.list = list;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        if (view == null)
        {
            view = inflater.inflate(R.layout.item_article, parent, false);
        }

        HashMap<String, String> map = list.get(position);

        TextView Title = (TextView) view.findViewById(R.id.item_title);

        TextView ByWhom = (TextView) view.findViewById(R.id.item_bywhom);

        ImageView Img = (ImageView) view.findViewById(R.id.item_img);

        ProgressBar bar = (ProgressBar) view.findViewById(R.id.progressBar1);

        TextView TextUnderImg = (TextView) view
                .findViewById(R.id.item_text_under_imag);

        TextView Comments = (TextView) view.findViewById(R.id.item_comment);

        TextView TableView = (TextView) view.findViewById(R.id.item_tableview);

        TextView ReadMore = (TextView) view.findViewById(R.id.item_readmore);

        context.getSystemService(Context.CONNECTIVITY_SERVICE);// if you want these service you must have to call it using context.

        Title.setText(map.get("title"));

        ByWhom.setText(map.get("publishdate"));

        return view;
    }

}

不要在AsyncTask中持有Activity。如果该Activity在AsyncTask运行时被销毁(按下BACK按钮、配置更改等),那么AsyncTask将会持有一个无效的Activity。特别是,在doInBackground()中绝不要引用Activity,因为后台线程正在运行时,Activity处于不确定状态。在这种情况下,AsyncTask应该持有LayoutInflater和ConnectivityService。 - CommonsWare
我理解@CommonsWare,但是根据Adapter的使用方式,我对它的使用概念是否正确有疑问。 - Zar E Ahmer
哎呀,我需要更多的睡眠。对不起。在这种情况下,我可能仍然会传递LayoutInflater和ConnectivityService,但我不太担心任何问题。再次,我为我的错误道歉。 - CommonsWare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接