Context的使用(Using Context)
来源:互联网 发布:苹果电脑c语言编程软件 编辑:程序博客网 时间:2024/06/08 12:16
Context用于获取application状态信息。为Activities,Fragments,Services访问resource files,images,themes,styles以及external directory location。还能访问android内建服务(services),例如layout inflation,keyboard,查找content providers(内容提供器)。
在许多情况下需要context的时候,我们只是简单的在当前activity实例中进行传递。在另外情况下,在activity创建的对象里,例如adapters或者fragment,我们需要在activity实例的对象里进行传递。而在activity外(例如application或者service)时,我们可以使用“application”context来代替。
明确创建一个component
// Provide context if MyActivity is an internal activity.Intent intent = new Intent(context, MyActivity.class);startActivity(intent);
需要两个信息:
1、package name,表明包含该component的application;
2、该component的完整java类名。
创建一个View
TextView textView = new TextView(context);
context包含以下view所需的信息:
1、device的screen size和尺寸,用于dp,sp和像素之间转换;
2、样式属性;
3、onClick属性的activity参考
inflating an XML Layout File
为了inflate一个XML Layout文件进入memory,使用context取LayoutInflater。(???)
// 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);
恢复系统服务
从application发送notification,需要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 RemoteViewsNotification.Builder builder = new Notification.Builder(context).setContentTitle("custom title");notificationManager.notify(notificationId, builder.build());
application Vs Activity Context
<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">
Adapters
Array Adapter
当ListView构建adapters时,通常在layout inflation中调用getContext()。
if (convertView == null) { convertView = LayoutInflater .from(getContext()) .inflate(R.layout.item_user, parent, false); }
> If you instantiate the ArrayAdapter with the application context,however, you are likely to notice that the themes/styles are not beingapplied. For this reason, make sure you are passing in the Activitycontext in these cases.
- Context的使用(Using Context)
- Android 全局获取Context(随时随地的使用Context)
- Context——Context的使用(2)
- android的context使用,工具类context
- Context——Context的使用
- Context使用的技巧
- Context的使用
- Context的使用
- android Context的使用
- Context.LAYOUT_INFLATER_SERVICE的使用
- golang 的 context.Context
- <context:component-scan> 的使用
- context与getApplication()的使用
- android中context的使用
- hadoop的Context简单使用
- Android中Context的使用
- React 中 context 的使用
- golang Context包的使用
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- 从缓存角度说post和get的区别(摘抄)
- 准备做个门户网站--美丽邵阳(一)
- Linux 学习笔记 (四)Ubuntu14.04 解决上网问题安装无线网卡驱动
- golfing interface学习
- Context的使用(Using Context)
- 命令与文件的查询 which whereis locate find
- 学习笔记之eclipse中处理js文件报错
- cisco命令行手册
- <仅是自己做笔记。。。系列-13>输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 数组
- 关于们MySQL 插入空值" '' " 与null
- Java基础知识 二(关键字、标识符、注释、数据类型、数据类型的转换)
- android 总结学习 简单的UI控件