Android Context.startService(intent)或startService(intent)

Android Context.startService(intent)或startService(intent),android,service,Android,Service,Context.startService(intent)和startService(intent)之间有什么区别?使用哪种方法重要吗?只有一种startService()方法startService()是上下文类上的一种方法,可用于上下文的所有子类,如活动或服务,正如Commonware所说,只有一个startService()。这就是Context.startService(intent) 您的主活动程序本身是上下文的一个实例,您不需要使用上下文明确地调用方法(startService) 这就

Context.startService(intent)
startService(intent)
之间有什么区别?使用哪种方法重要吗?

只有一种
startService()
方法
startService()
上下文
类上的一种方法,可用于
上下文
的所有子类,如
活动
服务
,正如Commonware所说,只有一个
startService()
。这就是Context.startService(intent)

您的主活动程序本身是
上下文
的一个实例,您不需要使用
上下文
明确地调用方法(startService)


这就像在类本身中调用类的方法。

解释

Android中的每个人都可能知道如何使用适配器。我们可以为他们创建单独的类。这将使我们的编码更易于处理和理解。但是当我们分别创建这些类时。他们需要一个上下文
代表调用
)。因此,在这种情况下,我们在其构造函数中传递活动的上下文。通过这种方式,Android知道我们是代表哪个活动来调用它的

我们不能打电话

getSystemService(Context...)//blah bhal
在单独的适配器类中,但我们可以在适配器构造函数中传递上下文,并可以这样调用它

context.getSystemService(Context....)//
像这样调用适配器

ArticleAdapter adapter = new ArticleAdapter(context, list);

                    list_of_article.setAdapter(adapter);
然后得到这样的上下文

context.getSystemService(Context....)//
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;
    }

}
公共类ArticleAdapter扩展了BaseAdapter
{
语境;
数组列表;
充气机;
公共ArticleAdapter(上下文,
ArrayList(列表)
{
this.context=上下文;
this.list=列表;
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount()
{
返回list.size();
}
@凌驾
公共对象getItem(int位置)
{
返回列表。获取(位置);
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图=转换视图;
如果(视图==null)
{
视图=充气机。充气(R.layout.item\u article,父项,false);
}
HashMap map=list.get(位置);
TextView Title=(TextView)view.findViewById(R.id.item_Title);
TextView bywho=(TextView)view.findViewById(R.id.item_bywho);
ImageView Img=(ImageView)view.findViewById(R.id.item\u Img);
ProgressBar=(ProgressBar)view.findViewById(R.id.progressBar1);
TextView TextUnderImg=(TextView)视图
.findviewbyd(R.id.item\u text\u在\u imag下);
TextView注释=(TextView)view.findViewById(R.id.item_注释);
TextView TableView=(TextView)view.findViewById(R.id.item\u TableView);
TextView ReadMore=(TextView)view.findViewById(R.id.item\u ReadMore);
context.getSystemService(context.CONNECTIVITY_SERVICE);//如果需要这些服务,必须使用context调用它。
Title.setText(map.get(“Title”);
bywho.setText(map.get(“publishdate”);
返回视图;
}
}

为什么有些人使用Context.startService(intent)而不是startService(intent)?你必须问他们。这是不必要的。请参阅下面我的答案,以了解为什么以及何时使用Context@mnish在
异步任务中不保留
活动
。如果在
AsyncTask
运行时该活动被销毁(后退按钮、配置更改等),则
AsyncTask
将保留无效的活动。特别是,切勿引用
doInBackground()
中的活动,因为后台线程运行时该活动处于不确定状态。在这种情况下,
AsyncTask
应该保留一个
LayoutInflater
和一个
ConnectivityService
。我理解@commonware,但根据适配器。我是否有在AdapterYesh中使用它的正确概念,我需要更多的睡眠。我道歉。在这种情况下,我可能仍然会通过
LayoutInflater
ConnectivityService
,但我不太关心任何问题。我再次为我的错误道歉。