你为什么要通过这种语境作为Android开发的参数? [英] Why do you have to pass this Context as a parameter in Android development?

查看:130
本文介绍了你为什么要通过这种语境作为Android开发的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它从一节课上developer.android.com

 公共无效的sendMessage(查看视图){
    意向意图=新的意图(这一点,DisplayMessageActivity.class);
    EditText上EDITTEXT =(EditText上)findViewById(R.id.edit_message);
    字符串消息= editText.getText()的toString()。
    intent.putExtra(EXTRA_MESSAGE,消息);
    startActivity(意向);
}
 

一个上下文作为第一个参数(这是因为Activity类是语境的一个子类)

=============

为什么需要'这个'?

解决方案
  

公开意图(上下文packageContext,CLS级)

     

参数

     

packageContext应用程序包的背景下实施这一   类。

     

CLS要被用于意图的组件类

正如你说得很有道理

 的java.lang.Object
   ↳android.content.Context //看到这
       ↳android.content.ContextWrapper
           ↳android.view.ContextThemeWrapper
               ↳android.app.Activity //看到这
 

您类扩展活动。

 公共类YouActivity扩展活动//延伸活动
 

因此​​指的是活动上下文的第一个参数的目的是一样的。

检查此链接

<一个href="http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html" rel="nofollow">http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html

不过,我会尽量解释好。

看一个源$ C ​​$ C

  3811
3812公共意向(上下文packageContext,类&LT;&GT;的cls){
3813 mComponent =新的组件名(packageContext,CLS);
3814}
 

然后

  61
62公共组件名(上下文PKG,串CLS){
63,如果(CLS == NULL)抛出新的NullPointerException(类的名字为空);
64 mPackage = pkg.getPackageName(); //看到这
65 mClass = CLS;
66}
 

getPackageName()的方法上下文

 公共抽象字符串getPackageName()

新增的API级别1
返回此应用程序的包的名称。
 

请注意

 意向意图=新的意图(这一点,DisplayMessageActivity.class);
 

这是明确的意图。您通常会使用一个明确的意图,在自己的应用程序启动组件,因为你知道你要启动的活动或服务的类名。

我包括克里斯·斯特拉顿的评论,他解释说好,为什么你需要一个上下文作为第一个参数。

  

有创建意图不要求语境的方式。   但是,如果你想针对特定类中的特定软件包,然后   提供上下文目标包是一个现成的办法做到这一点。   注意,上述code利用的事实是,目标类是   在同一个包作为发件人,因此发件人(本)减少时,   一篮子背景下也将是合适的包装方面   目标。这不会总是这种情况。

进一步阅读显性与隐性Intetns @

http://developer.android.com/guide/components/intents- filters.html

和还检查

http://developer.android.com/training/basics/intents/ sending.html

Its from a lesson on developer.android.com

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

A Context as its first parameter (this is used because the Activity class is a subclass of Context)

=============

Why do you need 'this'?

解决方案

public Intent (Context packageContext, Class cls)

Parameters

packageContext A Context of the application package implementing this class.

cls The component class that is to be used for the intent.

As you rightly said

java.lang.Object
   ↳    android.content.Context                 //see this 
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity    // see this 

Your class extends Activity.

public class YouActivity extends Activity // extends Activity

Hence this refers to Activity Context and the first param for intent is the same.

Check this link

http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html

However i will try to explain better.

Look a the source code

3811
3812    public Intent(Context packageContext, Class<?> cls) {
3813        mComponent = new ComponentName(packageContext, cls);
3814    }

And then

61 
62     public ComponentName(Context pkg, String cls) {
63         if (cls == null) throw new NullPointerException("class name is null");
64         mPackage = pkg.getPackageName();  // see this
65         mClass = cls; 
66     }

And getPackageName() is method of Context.

public abstract String getPackageName ()

Added in API level 1
Return the name of this application's package.

Note

Intent intent = new Intent(this, DisplayMessageActivity.class);

This is explicit intent. You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start.

I am including Chris Stratton's comment as he explains better as to why you need a context as the first param.

There are ways of creating an Intent which do not require a Context. But if you want to target a specific class of a specific package, then providing a context for the target package is a ready way to do that. Note that the above code leverages the fact that the target class is in the same package as the sender, so the sender ("this") when reduced to a package context will also be the proper package context for the target. That won't always be the case.

Further read Explicit and Implicit Intetns @

http://developer.android.com/guide/components/intents-filters.html

And also check

http://developer.android.com/training/basics/intents/sending.html

这篇关于你为什么要通过这种语境作为Android开发的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆