- Package visibility in Android 11
- Android Application Class
- Android Activity Lifecycle
- Android activity lifecycle is first thing to be known as android developer. Here I’m sharing with you some facts and…
- Android Application Launch explained: from Zygote to your Activity.onCreate()
- When does Android process start?
- Zygote : Spawning new life, new process
- When a user clicks an app icon in Launcher …
- There are three distinct phases of process launch :
- Thanks for reading through 🙌🏼. If you found this post useful, please applaud using the 👏 button and share it through your circles.
- Where Android apps store data?
- 3 Answers 3
Package visibility in Android 11
On Android 10 and earlier, apps could query the full list of installed apps on the system using methods like queryIntentActivities() . In most cases, this is far broader access than is necessary for an app to implement its functionality. With our ongoing focus on privacy, we’re introducing changes on how apps can query and interact with other installed apps on the same device on Android 11. In particular, we’re bringing better scoped access to the list of apps installed on a given device.
To provide better accountability for access to installed apps on a device, apps targeting Android 11 (API level 30) will see a filtered list of installed apps by default. In order to access a broader list of installed apps, an app can specify information about apps they need to query and interact with directly. This can be done by adding a element in the Android manifest.
For most common scenarios, including any implicit intents started with startActivity() , you won’t have to change anything! For other scenarios, like opening a specific third party application directly from your UI, developers will have to explicitly list the application package names or intent filter signatures like this:
If you use Custom Tabs to open URLs, you might be calling resolveActivity() and queryIntentActivities() in order to launch a non-browser app if one is available for the URL. In Android 11 there’s a better way to do this, which avoids the need to query other apps: the FLAG_ACTIVITY_REQUIRE_NON_BROWSER intent flag. When you call startActivity() with this flag, an ActivityNotFoundException will be thrown if a browser would have been launched. When this happens, you can open the URL in a Custom Tab instead.
In rare cases, your app might need to query or interact with all installed apps on a device, independent of the components they contain. To allow your app to see all other installed apps, Android 11 introduces the QUERY_ALL_PACKAGES permission. In an upcoming Google Play policy update, look for guidelines for apps that need the QUERY_ALL_PACKAGES permission.
When targeting API level 30 and adding a element to your app, use the latest available release of the Android Gradle plugin. Soon we’ll be releasing updates to older Android Gradle plugin versions to add support for this element. You can find more information and use cases about Package Visibility in the developer documentation.
Источник
Android Application Class
W hile Starting App development, we tend to miss out simple basic stuffs either by ignorance or by curiosity to build million dollar app. But, Hey! Why so serious !. Building a App is bit of Art ,bit of Engineering and frequently both.
Activity Life Cycle is stages of activity in run time, knowing these would save you from headaches while you dive deeper in development.
I have written a post that will help you to understand activity lifecycle in a practical approach. Check it out
Android Activity Lifecycle
Android activity lifecycle is first thing to be known as android developer. Here I’m sharing with you some facts and…
Application class is a base class of Android app containing components like Activities and Services. Application or its sub classes are instantiated before all the activities or any other application objects have been created in Android app.
You Don’t have to import or extend application class, they are predefined. We cannot change application class but we could give additional instruction to it by extending it. Refer here for more info.
Create a java class named SubApplication and Application as Superclass
By extending Application class you could get boilerplate code like this
Check in AndroidManifest.xml and set Application name to SubApplication that you created
Lets take a situation where we should know which activity is currently running and we have to use register network receiver in all our activities. To this we used to write same code in all the activities or write a base class and extend that class instead of extending AppCompactActivity.
We have Activity Life cycle in one hand and Application class in another, what sense they make? Well actually they do? Let’s look into it.
- In application class create static Activity variable it is accessible from whole project.
2. Register Activity Life Cycle callback in onCreate method in application class. By this step we can get currently running activity in our app from mActivity.
3.Moving on to next, Create a Broadcast Receiver and write a method to check the internet connection. you can get the code here .
4. Register your broadcast receiver in Manifest File
But for SDK Above Nougat we need to register receiver and unregister in every activity we use programmatically or We can just register and unregister Commonly in Application class.
5. Create a Object for Broadcast Receiver in application class and Register it in onResume method and Unregister in onPause method.
Bit confused! Don’t Worry, Here is the Link to the files.
Thanks for reading out the article. Let me know if I have missed out something interesting so that I can be added. Be sure to clap/recommend as much as you can and also share with your friends.
Источник
Android Application Launch explained: from Zygote to your Activity.onCreate()
Apr 30, 2018 · 6 min read
This article explains how Android launches your application when a user clicks an app icon. The Android system does a lot of heavy lifting behind the curtains to make your launch activity visible to a user. This article covers this process in detail by highlighting the important phases and call sequences.
Android applications are unique in two ways:
- Multiple Entry points: Android apps are composed of different components and they can invoke the components owned by other apps. These components roughly correspond to multiple entry points for any application. Hence, they differ from traditional applications which have a single entry point like main() method.
- Own Little World: Every Android application lives in its own world, it runs in a separate process, it has its own Dalvik VM instance and is assigned a unique user ID.
When does Android process start?
An Android process is started whenever it is required.
Any time a user or some other sys t em component requests a component (could be a service, an activity or an intent receiver) that belongs to your application be executed, the Android system spins off a new process for your app if it’s not already running. Generally processes keep running until killed by the system. Application processes are created on demand and a lot of things happen before you see your application’s launch activity up and running.
Every app runs in its own process: By default, every Android app runs in its own Android process which is nothing but a Linux process which gets one execution thread to start with. For example, when you click on a hyper-link in your e-mail, a web page opens in a browser window. Your mail client and the browser are two separate apps and they run in their two separate individual processes. The click event causes Android platform to launch a new process so that it can instantiate the browser activity in the context of its own process. The same holds good for any other component in an application.
Zygote : Spawning new life, new process
Let’s step back for a moment and have a quick look on system start-up process. Like the most Linux based systems, at startup, the boot loader loads the kernel and starts the init process. The init then spawns the low level Linux processes called “daemons” e.g. android debug daemon, USB daemon etc. These daemons typically handle the low level hardware interfaces including radio interface.
Init process then starts a very interesting process called ‘ Zygote ’.
As the name implies it’s the very beginning for the rest of the Android application. This is the process which initializes a very first instance of Dalvik virtual machine. It also pre-loads all common classes used by Android application framework and various apps installed on a system. It thus prepares itself to be replicated. It stats listening on a socket interface for future requests to spawn off new virtual machines (VM)for managing new application processes. On receiving a new request, it forks itself to create a new process which gets a pre-initialized VM instance.
After zygote, init starts the runtime process.
The zygote then forks to start a well managed process called system server. System server starts all core platform services e.g activity manager service and hardware services in its own context.
At this point the full stack is ready to launch the first app process — Home app which displays the home screen also known as Launcher application.
When a user clicks an app icon in Launcher …
The click event gets translated into startActivity(intent) and it is routed to ActivityManagerService via Binder IPC. The ActvityManagerService performs multiple steps
- The first step is to collect information about the target of the intent object. This is done by using resolveIntent() method on PackageManager object. PackageManager.MATCH_DEFAULT_ONLY and PackageManager.GET_SHARED_LIBRARY_FILES flags are used by default.
- The target information is saved back into the intent object to avoid re-doing this step.
- Next important step is to check if user has enough privileges to invoke the target component of the intent. This is done by calling grantUriPermissionLocked() method.
- If user has enough permissions, ActivityManagerService checks if the target activity requires to be launched in a new task. The task creation depends on Intent flags such as FLAG_ACTIVITY_NEW_TASK and other flags such as FLAG_ACTIVITY_CLEAR_TOP.
- Now, it’s the time to check if the ProcessRecord already exists for the process.If the ProcessRecord is null, the ActivityManager has to create a new process to instantiate the target component.
As you saw above many things happen behind the scene when a user clicks on an icon and a new application gets launched. Here is the full picture :
There are three distinct phases of process launch :
- Process Creation
- Binding Application
- Launching Activity / Starting Service / Invoking intent receiver …
ActivityManagerService creates a new process by invoking startProcessLocked() method which sends arguments to Zygote process over the socket connection. Zygote forks itself and calls ZygoteInit.main() which then instantiates ActivityThread object and returns a process id of a newly created process.
Every process gets one thread by default. The main thread has a Looper instance to handle messages from a message queue and it calls Looper.loop() in its every iteration of run() method. It’s the job of a Looper to pop off the messages from message queue and invoke the corresponding methods to handle them. ActivityThread then starts the message loop by calling Looper.prepareLoop() and Looper.loop() subsequently.
The following sequence captures the call sequence in detail —
The next step is to attach this newly created process to a specific application. This is done by calling bindApplication() on the thread object. This method sends BIND_APPLICATION message to the message queue. This message is retrieved by the Handler object which then invokes handleMessage() method to trigger the message specific action — handleBindApplication(). This method invokes makeApplication() method which loads app specific classes into memory.
This call sequence is depicted in following figure.
Launching an Activity:
After the previous step, the system contains the process responsible for the application with application classes loaded in process’s private memory. The call sequence to launch an activity is common between a newly created process and an existing process.
The actual process of launching starts in realStartActivity() method which calls sheduleLaunchActivity() on the application thread object. This method sends LAUNCH_ACTIVITY message to the message queue. The message is handled by handleLaunchActivity() method as shown below.
Assuming that user clicks on Video Browser application. the call sequence to launch the activity is as shown in the figure.
The Activity starts its managed lifecycle with onCreate() method call. The activity comes to foreground with onRestart() call and starts interacting with the user with onStart() call.
Thanks for reading through 🙌🏼. If you found this post useful, please applaud using the 👏 button and share it through your circles.
This article was initially published as two part series (Part 1 and Part 2) on my blog ./ mult-core-dump in 2010. These articles have been cited in multiple documents including the very famous Introduction to the Android Graphics Pipeline
Источник
Where Android apps store data?
Could you list all possible directories where Android apps may store data, providing description what kind of data are stored in each directory?
3 Answers 3
All apps (root or not) have a default data directory, which is /data/data/
. By default, the apps databases, settings, and all other data go here. This directory is «private» to the app – which means no other app and not even the user can access data in it (without root permissions).
If an app expects huge amounts of data to be stored, or for other reasons wants to «be nice to internal storage», there’s a corresponding directory on the SDCard ( Android/data/
Apart from that, all apps can store data anywhere on the SDCard, as there are no restrictions — and many apps do so. They can use directory names freely (and they again do), which is what often makes it hard to decide what all that «junk» on the card is intended for, and what of it can be deleted.
Though, as Tom pointed out, root-apps could store their data almost everywhere on your device, they usually follow the same rules as other apps.
You can find a general explanation of the Android directory hierarchy in my answer here. For your specific question I might add some more details on the /data/data/
(and corresponding SD-part):
- databases/ : here go the app’s databases
- lib/ : libraries and helpers for the app
- files/ : other related files
- shared_prefs/ : preferences and settings
- cache/ : well, caches
There might be several more directories in this place, or fewer — it all depends on the app. In its own «home directory» (and that’s what it basically is, spoken Linux-wise) they can place files where they want. Usually, these files and directories are only accessible by the app itself (and root, of course) — other than those stored on the SDCard, which are accessible by all apps.
Some major changes occurred to storage in Android 4.4 (see Android’s Storage Journey). So the following is generally true for Android 4.4+ and particularly 6+.
This is from my detailed answer to How disk space is used on Android device?. Apps’ files are saved (by system and app itself) to internal and external storage under different categories.
All of the above paths on internal and external storage (primary and secondary) are app’s private directories which are accessible to respective app without requesting any permission. Apps can also create other directories (not explicitly available through APIs) in their private storage. All of these directories belonging to an app are deleted when the app is uninstalled.
Additionally apps can put their data anywhere on primary external storage (including some standard directories and other apps’ private directories) if WRITE_EXTERNAL_STORAGE permission is granted ( getExternalStorageDirectory returns /storage/emulated/ ). For secondary external storage and removable storage SAF is used. See details in How to save files to external SD card?.
However in Android 10 writing directly to primary external shared storage is deprecated ( getExternalStorageDirectory and getExternalStoragePublicDirectory are no more available). Apps need to use one of Android’s built-in content providers; either MediaStore (for media files) or SAF (for any other type of files).
/data paths may get replaced with /mnt/expand/[UUID] when using Adoptable Storage. /storage/emulated gets replaced with /storage/[UUID] when using secondary external storage (like SD card).
For multiple users/profiles is different, device owner is always 0 . /data/user/0 is a symlink to /data/data for historical reasons. Secondary external storage is only available to device owner.
OBB directory is shared among users/profiles (up to Android 9) to save space. FUSE/ sdcardfs always exposes /storage/emulated/obb as /storage/emulated/ /Android/obb .
/data/user_de is the Device Encrypted storage on FBE devices which lets certain apps run on boot without asking for user credentials.
/data/misc/profiles are used by ART for profile-guided compilation of app code.
Description of each directory is somewhat evident from names, details can be seen in API documentation.
Caches are cleared by OS when running low on storage, keeping apps exceeding the allotted quota on top.
Apps’ private files directories in external storage aren’t automatically scanned by MediaScanner but media directories are.
Caches and no_backup directories are not backed up to cloud. See official documentation.
Источник