Context

Context继承于 Object, 直接子类有两个,用的多的是ContextWrapper. 而ContextWrapper的子类有Service ,ContextThemeWrapper(它的子类就是Activity)。
Context是一个抽象类,我们经常会见到它作为一个参数出现,activity(各种activity) 和service都是其子类,都可以向上转型作为一个context来使用。
它有很多方法,这摘取一些。
关于Activity的方法
1.abstract void ———- startActivity(Intent intent)
Same as startActivity(Intent, Bundle) with no options specified.
2.abstract void ———- startActivity(Intent intent, Bundle options)
Launch a new activity.
3.abstract void ———- startActivities(Intent[] intents, Bundle options)
Launch multiple new activities.
4.abstract void ———- startActivities(Intent[] intents)
Same as startActivities(Intent[], Bundle) with no options specified.

关于服务的一些方法
1.abstract boolean ———- bindService(Intent service, ServiceConnection conn, int flags)
Connect to an application service, creating it if needed.
2.abstract ComponentName ———- startService(Intent service)
Request that a given application service be started.
3.abstract boolean ———- stopService(Intent service)
Request that a given application service be stopped.
4.abstract void ———- unbindService(ServiceConnection conn)
Disconnect from an application service.
关于广播的一些方法
注册广播:
1.abstract Intent ———- registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
Register a BroadcastReceiver to be run in the main activity thread.
2.abstract Intent ———- registerReceiver(BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)
Register to receive intent broadcasts, to run in the context of scheduler.
发送广播:
3.abstract void ———-sendBroadcast(Intent intent)
Broadcast the given intent to all interested BroadcastReceivers.
4.abstract void ———- sendBroadcastAsUser(Intent intent, UserHandle user)
Version of sendBroadcast(Intent) that allows you to specify the user the broadcast will be sent to.
5.abstract void———- sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission)
Version of sendBroadcast(Intent, String) that allows you to specify the user the broadcast will be sent to.
6.abstract void ———- sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras)
Version of sendBroadcast(Intent) that allows you to receive data back from the broadcast.
7.abstract void ———- sendOrderedBroadcast(Intent intent, String receiverPermission)
Broadcast the given intent to all interested BroadcastReceivers, delivering them one at a time to allow more preferred receivers to consume the broadcast before it is delivered to less preferred receivers.
注销广播:
8.abstract void ———- unregisterReceiver(BroadcastReceiver receiver)
Unregister a previously registered BroadcastReceiver.
得到系统服务
abstract Object ———- getSystemService(String name)
Return the handle to a system-level service by name.
得到包名
abstract String ———- getPackageName()
Return the name of this application’s package.
得到包资源路径
abstract String ———- getPackageResourcePath()
Return the full ath to this context’s primary Android package.
得到资源
abstract Resources ———- getResources()
Return a Resources instance for your application’s package.
得到SharedPreferences
abstract SharedPreferences ———- getSharedPreferences(String name, int mode)
Retrieve and hold the contents of the preferences file ‘name’, returning a SharedPreferences through which you can retrieve and modify its values.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值