Android Context

Context的概述


Context提供对应用程序状态信息的访问。它提供Activities、Fragments、和 Services去访问resource filesimagesthemes/styles和外部目录地址。它还允许访问Android的内置服务,例如layout inflation、keyboardcontent providers

在许多情况下,当需要Context时,我们只需要传递当前activity的实例即可。当我们位于在activity创建的内部对象中,例如adapters或fragments,我们需要将activity实例传递到这些对象中。当我们不在activity(在application或service中)的情况下,我们可以使用Application Context替代Context。

Context的用途


显示启动组件

// Provide context if MyActivity is an internal activity.
Intent intent = new Intent(context, MyActivity.class);
startActivity(intent);

当显示打开组件时,需要以下两个参数:

  • 包名称:标识应用程序包含的组件
  • 组件的完全限定的Java类名称

如果要启动一个内部组件,则可以传递Context,因为当前应用程序的包名称可以通过context.getPackageName()获取。

创建一个View

TextView textView = new TextView(context);

Context包含下列views所需的信息:

  • 设备屏幕大小和尺寸信息,用于将dp、sp转换为px
  • styled attributes
  • onClick属性的activity引用

创建一个XML布局文件

我们使用Context来获取LayoutInflater,以便将XML布局infalte到内存中

// A context is required when creating views.
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.my_layout, parent);

发送一个本地广播

当发送或注册广播的接收器时,我们使用Context来获取LocalBroadcastManager:

// The context contains a reference to the main Looper, 
// which manages the queue for the application's main thread.
Intent broadcastIntent = new Intent("custom-action");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

检索系统服务

从应用程序发送通知,需要NotificationManager系统服务:

// Context objects are able to fetch or start system services.
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

int notificationId = 1;

// Context is required to construct RemoteViews
Notification.Builder builder = new Notification.Builder(context).setContentTitle("custom title");

notificationManager.notify(notificationId, builder.build());

Application Context与Activity Context

虽然主题和样式通常使用Application Context,不过它们也能使用Activity Context定义。Activity可以具有与Application的其他部分不同的一组主题或样式。(例如,如果需要为某些页面禁用ActionBar)你会注意到在AndroidManifest.xml文件中通常在application标签中添加一个android:theme属性。我们还可以为activity标签指定不同的一个。

<application
         android:allowBackup="true" 
         android:icon="@mipmap/ic_launcher" 
         android:label="@string/app_name" 
         android:theme="@style/AppTheme" > 

       <activity 
         android:name=".MainActivity" 
         android:label="@string/app_name" 
         android:theme="@style/MyCustomTheme">

因此,了解Application Context和Activity Context各自的生命周期的持续时间很重要。大多数Views应该传递一个Activity Context,以获得应该使用什么主题、样式、尺寸的信息。如果没有为activity显示指定主题,则默认使用application指定的主题。

在大多数情况下,你应该使用Activity上下文。通常,当在Activity内部使用Context时,可以使用Java中的关键字this引用类的实例,下面的示例显示了如何使用此方法显示Toast消息:

public class MainActivity extends AppCompatActivity {

          @Override
          protected void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState); 
                   setContentView(R.layout.activity_main); 
                   Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
          }
}

匿名函数中使用Context

我们在实现监听器时使用匿名函数,Java中的关键字this应用于声明最近的类。在这些情况下,必须指定外部类MainActivity来引用Activity实例。

public class MainActivity extends AppCompatActivity {

          @Override
          protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  TextView tvTest = (TextView) findViewById(R.id.abc);
                  tvTest.setOnClickListener(new View.OnClickListener() {
                         @Override 
                          public void onClick(View view) { 
                                 Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show(); 
                          } 
                    });
           }
}

Adapters中Context的使用

Array Adapter

当为ListView构建适配器时,通常在布局inflation时使用getContext()。通常使用此方法构造ArrayAdapter:

if (convertView == null) { 
        convertView = 
                LayoutInflater 
                         .from(getContext()) 
                         .inflate(R.layout.item_user, parent, false); }

如果你使用Application Context实例化ArrayAdapter,则需注意到主题/样式没有应用。因此,请确保你在这些情况下传递了Activity Context。

RecyclerView Adapter

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {

          @Override 
          public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                  View v = LayoutInflater .from(parent.getContext()) .inflate(itemLayout, parent, false); 

                  return new ViewHolder(v);
         }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int i) {
                  // If a context is needed, it can be retrieved  
                  // from the ViewHolder's root view.
                  Context context = viewHolder.itemView.getContext();

                  // Dynamically add a view using the context provided.
                  if(i == 0) {
                         TextView tvMessage = new TextView(context); 
                          tvMessage.setText("Only displayed for the first item.") ;

                          viewHolder.customViewGroup.addView(tvMessage);
                  }
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
                  public FrameLayout customViewGroup;

                  public ViewHolder(view imageView) {
                         // Very important to call the parent constructor
                        // as this ensures that the imageView field is populated.
                        super(imageView);

                        // Perform other view lookups.
                        customViewGroup = (FrameLayout) imageView.findById(R.id.customViewGroup);
                  }
      }
}

不管何时,ArrayAdapter都需要一个Context来传递给它的构造函数,而RecyclerView.Adapter不需要,当需要inflation时,可以从父视图得到正确的Context。

如果在onCreateViewHolder()方法外部需要Context,只要有一个ViewHolder实例即可,就可以通过viewHolder.itemView.getContext()来获取Context。itemView是ViewHolder基类中的public、non-null、final字段。

----段

避免内存泄露

Application Context通常需要在创建单例时使用,例如在多个Activities中都需要通过Context来获取系统服务的自定义管理器类。当不再运行后,由于保留对Activity Context的引用将导致内存不回收,因此需要使用Application Context替代。

在下面的例子中,如果所存储的Context是一个Activity或者Service,当它们被系统销毁,因为CustomManager类包含一个静态引用,则不能够进行垃圾回收。

pubic class CustomManager {
        private static CustomManager sInstance;

        public static CustomManager getInstance(Context context) {
                if (sInstance == null) {

                    // This class will hold a reference to the context
                    // until it's unloaded. The context could be an Activity or Service.

                    sInstance = new CustomManager(context);
              }

              return sInstance;
        }

        private Context mContext;

        private CustomManager(Context context) {
                    mContext = context;
        }
}

正确存储Context


为了避免内存泄露,不要持有超过Context生命周期的引用。检查你的后台线程、pending handlers或内部类等任何可能持有Context的对象。

正确的方法是将Application Context存储在CustomManager.getInstance()中。Application Context是一个单例,并与应用程序进程的生命周期相关联,所以可以安全地存储对它的引用。

当在组件的生命周期之外又或是独立于传递的Context的生命周期需要使用到Context时,则使用Application Context。

public static CustomManager getInstance(Context context) {
        if (sInstance == null) {

            // When storing a reference to a context, use the application context.
            // Never store the context itself, which could be a component.
            sInstance = new CustomManager(context.getApplicationContext());
        }
        
        return sInstance;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容