我希望能够在Android KeyStore中安全地存储一些敏感字符串.我从服务器获取字符串但我有一个用例需要我坚持它们.KeyStore只允许从与分配给我的应用程序的UID相同的UID进行访问,并且它将使用设备主密码加密数据,因此我的理解是我不需要进行任何额外的加密来保护我的数据.我的麻烦是,我遗漏了一些关于如何写数据的事情.只要省略对KeyStore.store(null)的调用,我下面的代码就可以完美地工作.该代码失败,只要我将数据放入KeyStore后就无法存储,那么我就无法坚持下去.
我想我错过了关于KeyStore API的一些内容,但我不知道是什么.任何帮助赞赏!
String metaKey = "ourSecretKey";
String encodedKey = "this is supposed to be a secret";
byte[] encodedKeyBytes = new byte[(int)encodedKey.length()];
encodedKeyBytes = encodedKey.getBytes("UTF-8");
KeyStoreParameter ksp = null;
//String algorithm = "DES";
String algorithm = "DESede";
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKeySpec secretKeySpec = new SecretKeySpec(encodedKeyBytes, algorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(secretKeySpec);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey);
keyStore.setEntry(metaKey, secretKeyEntry, ksp);
keyStore.store(null);
String recoveredSecret = "";
if (keyStore.containsAlias(metaKey)) {
KeyStore.SecretKeyEntry recoveredEntry = (KeyStore.SecretKeyEntry)keyStore.getEntry(metaKey, ksp);
byte[] bytes …Run Code Online (Sandbox Code Playgroud) 当我从Android Studio(版本2.1预览版1)打开AVD管理器时,我的AVD列表上会显示一条横幅,上面写着:
System image update is available (link:)"Update System Images"
当我点击"更新系统映像"链接时,我收到以下神秘错误消息:
**Packages Unavailable**
The following packages are not available:
- Package id system-images;android-MNC;default;x86_64
- Package id system-images;android-MNC;default;x86
Run Code Online (Sandbox Code Playgroud)
我不知道这意味着什么或如何解决它.网络搜索毫无结果.有任何想法吗?
与此处提出的问题类似:Android Studio不支持gradle版本,但我使用的是兼容版本,但答案并没有解决我的问题.
我正在使用在Ubuntu 14.04 LTS下运行的Android Studio Beta 0.8.1.我正在尝试导入一个与我的团队共享的项目,但是当我克隆项目并尝试构建它时,我收到此错误:
Error:The project is using an unsupported version of the Android Gradle
plug-in (0.11.2) <a href="fixGradleElements">Fix plug-in version and re-import
project</a>
Run Code Online (Sandbox Code Playgroud)
当我点击链接时,我收到此错误:
12:21:30 PM Quick Fix Failed
Unable to find any references to the Android Gradle plug-in in build.gradle files.
Please click the link to perform a textual search and then update the build files manually.
Run Code Online (Sandbox Code Playgroud)
这是我的build.gradle文件的相关部分:
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
}
}
Run Code Online (Sandbox Code Playgroud)
我已经手动安装了最新版本的Gradle以试图纠正这个问题(Ubuntu真的只想让我拥有版本1.4,但我们的项目是为Gradle 1.11+配置的.这是"gradle -v"的输出:
------------------------------------------------------------
Gradle …Run Code Online (Sandbox Code Playgroud) android gradle android-studio build.gradle android-gradle-plugin
我的Android应用程序管理多个片段.我看到该字段中有大量崩溃,但是包含此日志行:
java.lang.IllegalStateException:失败保存状态:active XxxFragment {81e598 id = 0x7f0b0069 tag_yyy}已清除索引:-1
在Stack Overflow中搜索答案毫无结果; 我似乎有很多公司想知道这个例外究竟意味着什么.深入研究异常跟踪和Android源代码,我可以看到异常来自我的主Activity保存其状态(FragmentActivity.onSaveInstanceState),并且各个Fragments正被写入Parcelable.每个Fragment都有一个索引(称为mIndex),它必须是非负的,但是从代码中可以清楚地知道为什么必须是这种情况,因为mIndex永远不会在该函数中再次使用.
我不知道Fragment如何进入这种状态,或者我能做些什么.我无法在自己的测试环境中重现错误.任何人都可以阐明如何避免和/或处理此异常?
java.lang.IllegalStateException:失败保存状态:active已清除片段中的索引
我正在尝试自定义 的行为AutoCompleteTextView,但遇到了一些困难。我的AutoCompleteTextView子类是一个多行编辑文本,我希望能够垂直定位下拉列表(实际上是 a ListPopupWindow)以便跟随用户的光标。
我想,如果我可以计算锚视图顶部和当前行位置之间的差异,我可以相应地设置偏移量,但我一定是做错了什么,因为下拉菜单并没有完全到我的位置想要它去。有人可以向我指出我在这段代码中做错了什么吗?
@Override
public void showDropDown() {
// Get the screen position of the cursor and set the vertical offset accordingly.
int[] screenPoint = new int[2];
getLocationOnScreen(screenPoint);
final Rect displayFrame = new Rect();
getWindowVisibleDisplayFrame(displayFrame);
int pos = getSelectionStart();
Layout layout = getLayout();
int line = layout.getLineForOffset(pos);
int lineTop = layout.getLineTop(Math.max(0,line - 1));
int lineBottom = layout.getLineBottom(line);
int availableSpaceAboveField = screenPoint[1] + lineTop - displayFrame.top;
int availableSpaceBelowField = displayFrame.bottom - screenPoint[1] - lineBottom; …Run Code Online (Sandbox Code Playgroud) 我有一个包含两个TextView的LinearLayout.让第一个TextView的文本为"短文本",第二个TextView的文本为"(s)".
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/variable-size"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ellipsize="middle"
android:lines="1"
android:singleLine="true"
android:text="short text"/>
<TextView
android:id="@+id/fixed-size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="none"
android:lines="1"
android:singleLine="true"
android:text="(s)" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我希望LinearLayout对用户显示:
[[short text][(s)]____________]
其中____意味着空视图.
现在,如果我在第一个TextView中添加一个稍长的字符串,我想看到这个:
[[slightly longer text][(s)]__]
如果我将更长的字符串放入第一个TextView,我想看到这个:
[[really long ...ng text][(s)]]
但我似乎无法找到一种方法来阻止第一个TextView完全挤出第二个TextView,如下所示:
[[really long lo... long text]]
我如何得到我正在寻找的行为?
我正在运行Android Studio 2.0 Beta 3.我有一个依赖于外部库的项目.当我使用Android Studio构建时,我收到以下错误消息:
($my_project_path)/build/intermediates/manifest/tmp/manifestMerger354333932548019277.xml:17:9-36 Error:
Attribute application@allowBackup value=(false) from [debug] AndroidManifest.xml:17:9-36
is also present at [($library)] AndroidManifest.xml:12:9-35 value=(true)
Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at manifestMerger354333932548019277.xml:7:5-9:19 to override
Error:Execution failed for task ':$(project):processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : Attribute application@allowBackup value=(false) from [debug] AndroidManifest.xml:17:9-36
is also present at [($library)] AndroidManifest.xml:12:9-35 value=(true)
Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at manifestMerger354333932548019277.xml:7:5-9:19 to override
Run Code Online (Sandbox Code Playgroud)
这很奇怪.Android Studio建议我编辑中间构建步骤的产品,以便完成编译?另外,当我查看有问题的源AndroidManifest.xml时,上层已将"tools-replace"属性定义为"android:allowBackup".简而言之,我不知道为什么我应该得到这个消息.此外,我的构建在命令行中运行良好.有什么建议?
编辑基于评论:一些评论者注意到我需要按照错误消息中的非常明确的指示,只有这些评论者错过了错误消息指示我编辑中间构建产品.
要非常明确:中间构建产品不是构建系统应该永远指导用户修改的东西.根据定义,仅仅是构建系统的使用.它唯一的用途是保存状态,以便用户可以对错误的构建进行故障排除.Android Studio告诉我修改此文件的事实证明,在Android Studio中,或者更有可能在我的配置和/或构建脚本中,某些内容严重受损.但是,什么?
为了澄清,这里是<application>项目中包含库的元素:
<application
android:label="@string/app_name"
android:allowBackup="false"
tools:replace="android:allowBackup">
<...> …Run Code Online (Sandbox Code Playgroud) 我正在尝试从我的应用程序中确定我的Android设备的存储加密状态.按照相关Android Developer页面的建议,这是我的代码:
DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
int encryptionStatus = mDPM.getStorageEncryptionStatus();
if (DEBUG) Log.v(TAG, "checkSecuritySettingsSufficient: encryptionStatus=" + encryptionStatus);
Run Code Online (Sandbox Code Playgroud)
这里的麻烦:当我在设备上运行的代码(我已经试过了在摩托罗拉Droid Maxx的运行Android 4.4.4及的Nexus 7运行的是Android 5.0.2),这我以前加密的,DevicePolicyManager.getStorageEncryptionStatus()将始终返回值1,即ENCRYPTION_STATUS_INACTIVE.
因此,Android报告该设备未加密,尽管文件系统肯定是加密的(我从Settings > Security页面检查了它的状态).
这个功能坏了吗?在SO或其他网络资源上似乎没有任何提及.这让我相信我没有做正确的事情DevicePolicyManager.
更新在使用Motorola设备再次执行加密步骤后,DevicePolicyManager.getStorageEncryptionStatus()返回正确的值,但它仍然在Nexus 7上失败.