Context.startService (намерение) или startService (намерение) - PullRequest
4 голосов
/ 15 мая 2010

В чем разница между Context.startService(intent) и startService(intent) и имеет ли значение, какой из них используется?

Ответы [ 3 ]

7 голосов
/ 15 мая 2010

Существует только один startService() метод. startService() - это метод класса Context, доступный для всех подклассов Context, например Activity или Service.

2 голосов
/ 29 января 2012

Как сказал Commonsware, есть только один startService(). Это Context.startService(intent).

Сама ваша основная программа действий является экземпляром Context, и вам не нужно подробно вызывать метод (startService) с Context.

Это похоже на вызов метода класса внутри самого класса.

0 голосов
/ 17 июля 2014

Пояснение

Все в Android могут знать, как использовать Adapter.Мы можем создать отдельный класс для них.это сделает наше кодирование более простым в обращении и понимании.Но когда мы создаем эти классы отдельно.Им нужен контекст (Calling on behalf).Таким образом, в этом случае мы передаем контекст действия в их конструктор.Таким образом, Android знает, что мы вызываем это от имени какого действия.

Мы не можем вызвать

getSystemService(Context...)//blah bhal

в отдельном классе адаптера, но мы можем передать контекст в конструкторе Adapterи может называть это следующим образом ..

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

Назовите свой адаптер следующим образом:

ArticleAdapter adapter = new ArticleAdapter(context, list);

                    list_of_article.setAdapter(adapter);

и получите контекст, подобный этому.класс

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

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...