博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android逆向分析概述_Android存储概述
阅读量:2524 次
发布时间:2019-05-11

本文共 8461 字,大约阅读时间需要 28 分钟。

android逆向分析概述

Storage is this thing we are all aware of, but always take for granted. Not long ago, every leap in storage capacity was incremental and appeared to be impossible. Nowadays, we don’t give a second thought when contemplating how much of it our devices have (and couldn't care less about the differences).

存储是我们都知道的事情,但始终是理所当然的。 不久前,存储容量的每一次飞跃都是递增的,似乎是不可能的。 如今,当我们考虑我们的设备有多少(并且不会在乎差异)时,我们不会再三思。

A bigger point would be to look at the evolution of what is stored in memory. Before smartphones, we saved the occasional photo or two, some games and a ton of text messages. But now, any standard phone will have a concoction of applications, documents, photos, videos, music files, and more. Let’s find out how we can utilize a devices's storage space for our applications.

更大的一点是要查看存储在存储器中的内容的演变。 在使用智能手机之前,我们偶尔会保存一两张照片,一些游戏和大量短信。 但是现在,任何标准手机都可以混合应用程序,文档,照片,视频,音乐文件等。 让我们找出如何为我们的应用程序利用设备的存储空间。

What we’re going to cover in this article is:

我们将在本文中介绍的是:

  1. The different types of storage on Android phones

    Android手机上的不同存储类型
  2. Differences between the types of storage

    存储类型之间的差异
  3. How to use storage in your application

    如何在应用程序中使用存储

Each application has access to two different types of storage: internal and external. There are major differences between these two types of storage, and knowing them will help you when designing your next application.

每个应用程序都可以访问两种不同类型的存储: internalexternal 。 这两种类型的存储之间存在主要差异,了解它们将在设计下一个应用程序时为您提供帮助。

Before we begin, one thing must be said about storage and cache. Storage is meant for things you want to save persistently, while cache is there to save things temporarily.

在开始之前,必须先谈一谈存储和缓存。 存储用于要永久保存的内容,而缓存则用于临时保存内容。

内部存储器 (Internal Storage)

When each application is run on the operating system, it has its own internal storage. This storage is private and only for the use of the application. Meaning, other applications cannot access it, nor can the user. Another thing to keep in mind when using internal storage is the availability of it. Unlike external storage, internal storage is always available for your application.

当每个应用程序在操作系统上运行时,它都有自己的内部存储。 此存储是私有的,仅供应用程序使用。 这意味着其他应用程序无法访问它,用户也无法访问它。 使用内部存储时要记住的另一件事是它的可用性。 与外部存储不同,内部存储始终可用于您的应用程序。

Using this storage has its drawbacks, though. If the user removes the application, all the data stored in your app's internal storage is removed as well. Imagine what would happen if you installed a game on your phone and somewhere down the road decided to remove it. You would like to have your game progress saved, if by any chance you would install the game again.

但是,使用此存储有其缺点。 如果用户删除该应用程序,则存储在您应用程序内部存储器中的所有数据也将被删除。 想象一下,如果您在手机上安装了游戏,但后来决定将其删除,将会发生什么情况。 您希望保存游戏进度,如果有机会再次安装游戏。

So, how do we save a file to internal storage?

那么,我们如何将文件保存到内部存储中?

As you can see in the code example, we are saving a file called hello_world_file that contains the text, “Hello From Internal Storage!”. I have created two catch clauses just to demonstrate the exceptions that may occur when trying to do this, but you can minimize them to one catch clause with the general Exception object.

在代码示例中可以看到,我们正在保存一个名为hello_world_file的文件,其中包含文本“ Hello From Internal Storage!”。 。 我创建了两个catch子句只是为了演示尝试执行此操作时可能发生的异常,但是您可以使用常规Exception对象将它们最小化为一个catch子句。

Pay attention that the method openFileOutput will open the file if it already exists, but if not, will create it. The second parameter to this method is the file mode. This parameter designates the scope of the file and access to it. The default value is MODE_PRIVATE, which makes the file accessible to your application only.

请注意,如果文件已经存在,则openFileOutput方法将打开该文件,但如果不存在,则将创建该文件。 此方法的第二个参数是文件模式。 此参数指定文件的范围和对其的访问。 默认值为MODE_PRIVATE,这使该文件只能由您的应用程序访问。

The other two values for this parameter are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE, but they have been deprecated since API 17. Sharing private files with other applications uses a different set of logic that you can read more about . Finally, when writing to the file, we convert our string to bytes and we make sure to close the file at the end.

此参数的其他两个值是MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE,但是自API 17开始不推荐使用。与其他应用程序共享私有文件使用一组不同的逻辑,您可以了解更多信息。 最后,在写入文件时,我们将字符串转换为字节,并确保最后关闭文件。

外置储存 (External Storage)

Contrary to what the name implies, this is storage that is defined by the fact that it is not always accessible. This may mean that it can be an external SD card (secondary external storage), but it can also be storage found on the device (primary external storage).

与名称所暗示的相反,这是由于存储并不总是可访问而定义的。 这可能意味着它可以是外部SD卡(辅助外部存储),但也可以是设备上的存储(辅助外部存储)。

To bring the fact home, external storage is storage that can be accessed when you connect your device to a computer via a USB cable. As you may have guessed, anything stored in this type of storage is accessible to other applications on your device, but will be kept if you uninstall the application.

为了将事实带回家,外部存储是当您通过USB电缆将设备连接到计算机时可以访问的存储。 您可能已经猜到,存储在该类型存储中的所有内容都可以被设备上的其他应用程序访问,但是如果您卸载该应用程序,则这些内容将保留。

Before we can demonstrate how to save files to the external storage, we need to do two things:

在演示如何将文件保存到外部存储之前,我们需要做两件事:

  1. Make sure there is enough space there to save the file

    确保那里有足够的空间来保存文件
  2. Ask for permission during runtime

    在运行时寻求许可

Making sure there is enough storage space requires the following lines of code:

确保有足够的存储空间,需要以下代码行:

To get access to the external storage, we need to add the following permission to our AndroidManifest.xml:

要访问外部存储,我们需要向AndroidManifest.xml添加以下权限:

Furthermore, since API 23, dangerous permissions aren’t authorized during install time, but during runtime. Writing to external storage is categorized as one, so we need to add logic to allow the user to decide whether to grant the application permission or not.

此外,由于使用API​​ 23,危险权限不会在安装时授权,而是在运行时授权。 写入外部存储归为一类,因此我们需要添加逻辑以允许用户决定是否授予应用程序许可。

Our writeFileToExternalStorage looks like this:

我们的writeFileToExternalStorage看起来像这样:

If you want to see an example of all the code presented here, you can head over to this .

如果您想查看此处提供的所有代码的示例,可以转到 。

很高兴知道 (Good To Know)

Above were just two simple examples of how to work with the different types of storage for your application. Since we are dealing with a resource that the system manages, we should also be aware of the behaviors associated with it.

上面仅是两个简单示例,说明如何为您的应用程序使用不同类型的存储。 由于我们正在处理系统管理的资源,因此我们还应该注意与之相关的行为。

By default, your application will be installed to internal storage (see internalOnly explanation), but from API level 8, you can add an attribute, installLocation, to your manifest that lets your application be installed to external storage. One reason for doing so is if your application is very large, and you would prefer the user to install it on the device’s external storage since there is more space there.

默认情况下,您的应用程序将安装到内部存储( 请参阅internalOnly说明 ),但是从API级别8开始,您可以在清单中添加属性installLocation ,以将应用程序安装到外部存储。 这样做的原因之一是,如果您的应用程序非常大,并且您希望用户将其安装在设备的外部存储上,因为那里有更多空间。

There are three values for this attribute:

此属性有三个值:

  • auto - means that you don’t have a specific preference where the application will be installed. The application will try to be installed to internal storage, but if it is full, it will install it inside external storage

    自动 -表示您没有安装应用程序的特定首选项。 该应用程序将尝试安装到内部存储中,但是如果已满,它将安装在外部存储中

  • internalOnly - the application will only be installed to internal storage, and if there isn’t enough space there, it will not be installed

    internalOnly-该应用程序将仅安装到内部存储中,并且如果那里没有足够的空间,则不会安装该应用程序

  • preferExternal - means that you want your application to be installed to external storage, but if there is not enough room there, it will be installed internally

    preferredExternal-表示您希望将应用程序安装到外部存储中,但是如果那里没有足够的空间,它将在内部安装

For both the auto and the preferExternal options, the user has the option of moving the application from external storage to internal, and vice versa.

对于auto和preferredExternal选项,用户可以选择将应用程序从外部存储移动到内部,反之亦然。

Keep in mind that once a user connects their device to a computer and enables it to share data or unmounts an SD card, all applications running from the external storage are destroyed. If your application uses one of the following features, you should not install it to external storage:

请记住,一旦用户将其设备连接到计算机并使其能够共享数据或卸载SD卡,从外部存储运行的所有应用程序都会被破坏。 如果您的应用程序使用以下功能之一,则不应将其安装到外部存储中:

Various services (alarm services in particular), Input Method Engines, Live Wallpapers, Application Widgets, Account Managers, Sync Adapters, Device Administrators and Broadcast receivers listening for boot completed.

各种服务( 尤其是 警报服务 ),输入法引擎,动态壁纸,应用程序小部件,帐户管理器,同步适配器,设备管理员和侦听引导的广播接收器已完成。

翻译自:

android逆向分析概述

转载地址:http://xjuzd.baihongyu.com/

你可能感兴趣的文章
CSS居中初探
查看>>
element-ui table 点击分页table滚动到顶部
查看>>
UVa 1585 Score 得分 题解
查看>>
洛谷 P2197 nim游戏
查看>>
Linux中对为本去重
查看>>
layui下拉框数据过万渲染渲染问题解决方案
查看>>
有序列表和无序列表
查看>>
Bootstrap文档
查看>>
【翻译】在Ext JS集成第三方库
查看>>
中华优秀传统文化教育的有效渗透
查看>>
计算机基础篇-01
查看>>
leetcode 58. Length of Last Word
查看>>
ios开发证书,描述文件,bundle ID的关系
查看>>
jsp之简单的用户登录系统(纯jsp)
查看>>
js计时事件
查看>>
EntityFramework 学习 一 Eager Loading
查看>>
dispatch_set_target_queue测试
查看>>
自己写的sql排序
查看>>
关于Mutex的笔记
查看>>
凸包1——卷包裹算法
查看>>