Context.startService (intent) or startService (intent)

What is the difference between Context.startService(intent) and startService(intent) and does it matter which one is used?

+4
source share
3 answers

There is only one startService() method. startService() is a method of the Context class that is available for all subclasses of Context , such as Activity or Service .

+7
source

As Commonsware says, there is only one startService() . This is Context.startService(intent) .

Your main activity program itself is an instance of Context , and you do not need to explicitly explain the (startService) method with Context .

This is like calling a class method inside the class itself.

+2
source

Explanation

Everyone on Android can know how to use the Adapter . We can create a separate class for them. this will simplify the processing and understanding of our coding. But when we create these classes separately. They need Context ( Calling on behalf ). Therefore, in this case, we pass the activity context in our constructor. Thus, Android knows that we invoke it on behalf of what activity.

We cannot name

 getSystemService(Context...)//blah bhal 

in a separate adapter class, but we can pass the context in the Adapter constructor and call it as follows.

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

Call your adapter as follows

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

and get a context like this.

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

Source: https://habr.com/ru/post/1309863/


All Articles