代码之家  ›  专栏  ›  技术社区  ›  mnish

Context.StartService(意向)或StartService(意向)

  •  4
  • mnish  · 技术社区  · 16 年前

    两者有什么区别 Context.startService(intent) startService(intent) 用哪一个重要吗?

    3 回复  |  直到 16 年前
        1
  •  7
  •   CommonsWare    16 年前

    只有一个 startService() 方法。 启动服务() 是上的方法 Context 类,可用于 语境 ,像 Activity Service .

        2
  •  2
  •   Alex Lockwood Nigel Hawkins    14 年前

    正如Commonware所说,只有一个 startService() . 那就是 Context.startService(intent) .

    您的主活动程序本身是 Context 您不需要使用 语境 .

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

        3
  •  0
  •   Zar E Ahmer    12 年前

    解释

    每个人都在 安卓 可能知道如何使用 Adapter . 我们可以为他们创建单独的类。这将使我们的编码更容易处理和理解。但是当我们单独创建这些类时。他们需要一个 语境 ( Calling on behalf )因此,在这种情况下,我们在它们的构造函数中传递活动的上下文。这样,Android就知道我们是代表哪个活动来调用它的。

    我们不能打电话

    getSystemService(Context...)//blah bhal
    

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

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

    像这样调用适配器

    ArticleAdapter adapter = new ArticleAdapter(context, list);
    
                        list_of_article.setAdapter(adapter);
    

    然后得到这样的上下文……

    文章适配器.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;
        }
    
    }