几个比较混淆概念,android中的Info类以及PackageManager和ActivityManager ... ...

 在Android开发过程中经常遇到以下几种类,对这些比较混淆,今天VincentTung 对其进行逐一认识和学习。

 

1.PackageManager

 

 

查了一下API,就一句话:

Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().

用来获取现在安装在设备上的程序包的各种信息的类,通过getPackageManager()方法获取。其实可以把Package看多Windows下的程序管理器。获取所有的在手机上安装的apk信息, 包名, 所有的activity, service,receiver, 

获取到的每一个应用程序的PackageInfo相当于是对某一个apk文件的java的包装.

能够获取程序包的那些信息呢?再去看以API中的方法,肯定是getXXX( )方法,看到N多getXXX( )方法,可以获取ActivityIcon 可以获取ActivityInfo、ApplicationIcon、ApplicationLable等等。下面主要看看一下几个方法

1.public abstract List<ApplicationInfo>  getInstalledApplications(int flags) 

  Return a List of all application packages that are installed on the device. 

返回设备上已经安装的所有的程序包的List

2.public abstract List<PackageInfo> getInstalledPackages (int flags) 

 Return a List of all packages that are installed on the device.

返回设备上已经安装的所有的包的List

 

2.ActivityManager

系统的一个服务,比较像 windows下的进程管理器,任务管理器,

可以获取所有运行的程序的信息,如正在运行的后台进程等等

3.Application

一般在共享全局数据的时候使用,要先继承Application类,根据需要重写相关方法,然后将AndroidManifest.xml中application标签修改成自己的application类

 

4.ApplicationContext  与 Context

上下文Context很常用。都知道它叫上下文,它到底是干什么的呢?

查一下API:Context 是 abstract class ,是一供了关于应用环境全局信息的接口它允许访问应用程序的资源(asset,res目录下的资源)和类。同时启动应用级的操作,如启动Activity,发送广播和接收intent

常用的方法

abstract void  startActivity(Intent intent);启动Activity

abstract void    sendBroadcast(Intent intent);发送广播,

abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter);注册广播

abstract void  unregisterReceiver(BroadcastReceiver receiver

)

abstract ComponentName startService(Intent service);开启关闭Service服务

abstract boolean stopService(Intent service)

 

abstract boolean bindService (Intent service, ServiceConnection conn, int flags);绑定服务

abstract void  unbindService(ServiceConnection conn);

 

 

abstract Context getApplicationContext() ; 获取ApplicationContext

abstract AssetManager getAssets();获取AssetManager,一般再通过AssetManager再去操作assets目录下的资源

abstract File getCacheDir();获取缓存目录

 

如果细心的同学会发现,其实Activity、Service与Application这三个有个相同点,就是都继承过Context类,所以每一个应用程序都有自己的context,就是ApplicationContext,可以通过getApplicationContext( )方法获取,其实这个ApplicationContext就是你在AndroidManifest.xml 总Application标签中指定的那个Application,为什么呢?看一下getApplicationContext()方法的API

public Context getApplicationContext ()

Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

看以看出调用这个方法返回的就是当前进程唯一的全局的Application对象。

所以小结一下:

Context有两种 1.ApplicationContext ,通过getApplicationContext( )得到,就是你在AndroidManifest.xml中指定的Application。这个Application一般什么时候用呢,就是我们在使用全局的变量的时候可以借助于Application

2.Activity 和Service都继承于Context,所以它们也是Context。

 

5.ApplicationInfo

AndroidManifest.xml 文件中<Application/>标签对应的相应信息

6.ActivityInfo

AndroidManifest.xml 文件中Activity 和Receiver相应信息

 

7.ServiceInfo

同上

8.PackageInfo

主要包含某个Package全部信息,这些信息就是这个Package对应的AndroidManifest.xml中的信息,通过PackageManager可以获取。

9. ResolveInfo

AndroidManifest.xml 文件中多有Intent-filter 标签对应的相应信息,

 

 

 

你可能感兴趣的:(android,activitymanager,packagemanager,Info类)