public class Application extends ContextWrapper implements ComponentCallbacks2
"android:name"
attribute in your
AndroidManifest.xml's <application>
tag. The Application
class, or your subclass of the Application class, is instantiated before any
other class when the process for your application/package is created.
Note: There is normally no need to subclass
Application. In most situations, static singletons can provide the same
functionality in a more modular way. If your singleton needs a global
context (for example to register broadcast receivers), include
Context.getApplicationContext()
as a Context
argument when invoking your singleton's
getInstance()
method.
Modifier and Type | Class and Description |
---|---|
static interface |
Application.ActivityLifecycleCallbacks |
static interface |
Application.OnProvideAssistDataListener
|
Context.BindServiceFlags, Context.CreatePackageOptions, Context.ServiceName
Modifier and Type | Field and Description |
---|---|
LoadedApk |
mLoadedApk |
ACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, APP_OPS_SERVICE, APPWIDGET_SERVICE, AUDIO_SERVICE, BACKUP_SERVICE, BATTERY_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_ALLOW_WHITELIST_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_EXTERNAL_SERVICE, BIND_FOREGROUND_SERVICE, BIND_FOREGROUND_SERVICE_WHILE_AWAKE, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_NOT_VISIBLE, BIND_SHOWING_UI, BIND_TREAT_LIKE_ACTIVITY, BIND_VISIBLE, BIND_WAIVE_PRIORITY, BLUETOOTH_SERVICE, CAMERA_SERVICE, CAPTIONING_SERVICE, CARRIER_CONFIG_SERVICE, CLIPBOARD_SERVICE, CONNECTIVITY_SERVICE, CONSUMER_IR_SERVICE, CONTEXT_CREDENTIAL_PROTECTED_STORAGE, CONTEXT_DEVICE_PROTECTED_STORAGE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_REGISTER_PACKAGE, CONTEXT_RESTRICTED, CONTEXTHUB_SERVICE, COUNTRY_DETECTOR, DEVICE_IDLE_CONTROLLER, DEVICE_POLICY_SERVICE, DISPLAY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, ETHERNET_SERVICE, FINGERPRINT_SERVICE, GATEKEEPER_SERVICE, HARDWARE_PROPERTIES_SERVICE, HDMI_CONTROL_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, JOB_SCHEDULER_SERVICE, KEYGUARD_SERVICE, LAUNCHER_APPS_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_PROJECTION_SERVICE, MEDIA_ROUTER_SERVICE, MEDIA_SESSION_SERVICE, MIDI_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_NO_LOCALIZED_COLLATORS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NETWORK_POLICY_SERVICE, NETWORK_SCORE_SERVICE, NETWORK_STATS_SERVICE, NETWORKMANAGEMENT_SERVICE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, PERSISTENT_DATA_BLOCK_SERVICE, POWER_SERVICE, PRINT_SERVICE, RADIO_SERVICE, RECOVERY_SERVICE, RESTRICTIONS_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, SERIAL_SERVICE, SHORTCUT_SERVICE, SIP_SERVICE, SOUND_TRIGGER_SERVICE, STATUS_BAR_SERVICE, STORAGE_SERVICE, SYSTEM_HEALTH_SERVICE, TELECOM_SERVICE, TELEPHONY_SERVICE, TELEPHONY_SUBSCRIPTION_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, TRUST_SERVICE, TV_INPUT_SERVICE, UI_MODE_SERVICE, UPDATE_LOCK_SERVICE, USAGE_STATS_SERVICE, USB_SERVICE, USER_SERVICE, VIBRATOR_SERVICE, VOICE_INTERACTION_MANAGER_SERVICE, WALLPAPER_SERVICE, WIFI_NAN_SERVICE, WIFI_P2P_SERVICE, WIFI_RTT_SERVICE, WIFI_SCANNING_SERVICE, WIFI_SERVICE, WINDOW_SERVICE
TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
Constructor and Description |
---|
Application() |
Modifier and Type | Method and Description |
---|---|
void |
onConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your
component is running.
|
void |
onCreate()
Called when the application is starting, before any activity, service,
or receiver objects (excluding content providers) have been created.
|
void |
onLowMemory()
This is called when the overall system is running low on memory, and
actively running processes should trim their memory usage.
|
void |
onTerminate()
This method is for use in emulated process environments.
|
void |
onTrimMemory(int level)
Called when the operating system has determined that it is a good
time for a process to trim unneeded memory from its process.
|
void |
registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) |
void |
registerComponentCallbacks(ComponentCallbacks callback)
Add a new
ComponentCallbacks to the base application of the
Context, which will be called at the same times as the ComponentCallbacks
methods of activities and other components are called. |
void |
registerOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback) |
void |
unregisterActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) |
void |
unregisterComponentCallbacks(ComponentCallbacks callback)
Remove a
ComponentCallbacks object that was previously registered
with Context.registerComponentCallbacks(ComponentCallbacks) . |
void |
unregisterOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback) |
attachBaseContext, bindService, bindServiceAsUser, canStartActivityForResult, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkPermission, checkSelfPermission, checkUriPermission, checkUriPermission, checkUriPermission, clearWallpaper, createApplicationContext, createConfigurationContext, createCredentialProtectedStorageContext, createDeviceProtectedStorageContext, createDisplayContext, createPackageContext, createPackageContextAsUser, databaseList, deleteDatabase, deleteFile, deleteSharedPreferences, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getBasePackageName, getCacheDir, getClassLoader, getCodeCacheDir, getContentResolver, getDatabasePath, getDataDir, getDir, getDisplay, getDisplayAdjustments, getExternalCacheDir, getExternalCacheDirs, getExternalFilesDir, getExternalFilesDirs, getExternalMediaDirs, getFilesDir, getFileStreamPath, getMainLooper, getNoBackupFilesDir, getObbDir, getObbDirs, getOpPackageName, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getSharedPreferences, getSharedPreferencesPath, getSystemService, getSystemServiceName, getTheme, getThemeResId, getUserId, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isCredentialProtectedStorage, isDeviceProtectedStorage, isRestricted, moveDatabaseFrom, moveSharedPreferencesFrom, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, registerReceiverAsUser, removeStickyBroadcast, removeStickyBroadcastAsUser, revokeUriPermission, sendBroadcast, sendBroadcast, sendBroadcast, sendBroadcast, sendBroadcastAsUser, sendBroadcastAsUser, sendBroadcastAsUser, sendBroadcastMultiplePermissions, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendOrderedBroadcastAsUser, sendOrderedBroadcastAsUser, sendOrderedBroadcastAsUser, sendStickyBroadcast, sendStickyBroadcastAsUser, sendStickyBroadcastAsUser, sendStickyOrderedBroadcast, sendStickyOrderedBroadcastAsUser, setTheme, setWallpaper, setWallpaper, startActivities, startActivities, startActivitiesAsUser, startActivity, startActivity, startActivityAsUser, startActivityAsUser, startActivityForResult, startInstrumentation, startIntentSender, startIntentSender, startService, startServiceAsUser, stopService, stopServiceAsUser, unbindService, unregisterReceiver
bindServiceAsUser, createCredentialEncryptedStorageContext, createDeviceEncryptedStorageContext, getColor, getColorStateList, getDrawable, getSharedPrefsFile, getString, getString, getSystemService, getText, isCredentialEncryptedStorage, isDeviceEncryptedStorage, migrateDatabaseFrom, migrateSharedPreferencesFrom, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes
public LoadedApk mLoadedApk
public void onCreate()
public void onTerminate()
public void onConfigurationChanged(Configuration newConfig)
ComponentCallbacks
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
For more information, read Handling Runtime Changes.
onConfigurationChanged
in interface ComponentCallbacks
newConfig
- The new device configuration.public void onLowMemory()
ComponentCallbacks
You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.
Preferably, you should implement ComponentCallbacks2.onTrimMemory(int)
from
ComponentCallbacks2
to incrementally unload your resources based on various
levels of memory demands. That API is available for API level 14 and higher, so you should
only use this ComponentCallbacks.onLowMemory()
method as a fallback for older versions, which can be
treated the same as ComponentCallbacks2.onTrimMemory(int)
with the ComponentCallbacks2.TRIM_MEMORY_COMPLETE
level.
onLowMemory
in interface ComponentCallbacks
public void onTrimMemory(int level)
ComponentCallbacks2
To retrieve the processes current trim level at any point, you can
use ActivityManager.getMyMemoryState(RunningAppProcessInfo)
.
onTrimMemory
in interface ComponentCallbacks2
level
- The context of the trim, giving a hint of the amount of
trimming the application may like to perform. May be
ComponentCallbacks2.TRIM_MEMORY_COMPLETE
, ComponentCallbacks2.TRIM_MEMORY_MODERATE
,
ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
, ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
,
ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
, ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
,
or ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
.public void registerComponentCallbacks(ComponentCallbacks callback)
Context
ComponentCallbacks
to the base application of the
Context, which will be called at the same times as the ComponentCallbacks
methods of activities and other components are called. Note that you
must be sure to use Context.unregisterComponentCallbacks(android.content.ComponentCallbacks)
when
appropriate in the future; this will not be removed for you.registerComponentCallbacks
in class Context
callback
- The interface to call. This can be either a
ComponentCallbacks
or ComponentCallbacks2
interface.public void unregisterComponentCallbacks(ComponentCallbacks callback)
Context
ComponentCallbacks
object that was previously registered
with Context.registerComponentCallbacks(ComponentCallbacks)
.unregisterComponentCallbacks
in class Context
public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
public void unregisterActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
public void registerOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)
public void unregisterOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)