小编Sne*_*dya的帖子

API等级26:粘性背景服务

由于Android O不允许在不通知用户的情况下启动后台服务,因此我想选择使用Firebase Jobdispatcher在后台运行新线程的不同方法,请参阅:https://github.com/firebase/firebase -jobdispatcher,机器人

在构建API 26之前,我曾经在服务中启动一个新线程.这是一个STICKY服务,并将在用户从最近关闭应用程序时重新启动(当幻灯片从正在运行的应用程序中删除应用程序时).

在下面的API 26之前查看我的原始代码.

public class NotificationService extends Service {

@Override
public void onCreate() {
    // this will start the thread
    ApplicationLoader.postInitApplication();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
Run Code Online (Sandbox Code Playgroud)

在API 26中,出现了一些限制:

https://developer.android.com/about/versions/oreo/android-8.0-changes.html#back-all

所以上面的方法不再起作用了.但即使用户从最近的应用程序关闭应用程序,我仍然希望我的线程能够运行.我发现了一种解决方法,但对我来说感觉非常糟糕.

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher
                (new GooglePlayDriver(applicationContext));

Job notificationJob = dispatcher.newJobBuilder()
          .setService(NotificationService.class)
          .setTag("backgroundJob-" + System.currentTimeMillis())
          .setRecurring(true)
          .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
          .setTrigger(Trigger.executionWindow(50, 60))
          .setReplaceCurrent(false)
          .build();
dispatcher.mustSchedule(notificationJob); …
Run Code Online (Sandbox Code Playgroud)

java multithreading android

5
推荐指数
0
解决办法
661
查看次数

Room Persistence Library:迁移过程中出现奇怪的错误

我因这个错误而挠头。到目前为止我找不到任何答案。我有旧数据库,我正在将其迁移到 Persistence Room 库。但是,每当我进行迁移时,都会出现以下错误,

java.lang.IllegalStateException: Migration didn't properly handle. 
Run Code Online (Sandbox Code Playgroud)

我使用的代码如下:

@Entity(tableName = ROUTE_TABLE)
public class RouteData {

    static final String ROUTE_TABLE = "name";

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    private int id;
    @ColumnInfo(name = "col1")
    private String col1;
    //Other columns with exactly same as 'col1' just different names
    //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

对于迁移,

Room.databaseBuilder(context.getApplicationContext(), MyData.class, DATABASE_NAME)
            .addMigrations(MIGRATION_LATEST)
            .fallbackToDestructiveMigration() 
            .build();

private static final Migration MIGRATION_LATEST = new Migration(9, 10) {
    @Override
    public void migrate(SupportSQLiteDatabase db) {
        //Log.i("Tag" , "Migration Started");
        //Migration …
Run Code Online (Sandbox Code Playgroud)

android database-migration android-room

5
推荐指数
1
解决办法
2612
查看次数

Firestore id 命名约定

FireStore 中的文档 ID 是否有任何命名约定。我在集合中创建了一些文档,以“yyyy-mm-dd”中的日期作为 ID。当我尝试获取集合中的所有文档时,我的快照大小为零。当我将一个带有 test 的虚拟文档作为 id 放置时,我只得到了测试文档而不是其他文档。

我的文档结构

火库结构

firebase google-cloud-firestore

5
推荐指数
1
解决办法
8121
查看次数

Android Studio会查找旧版本的构建工具

描述

在Android Studio 3.0.1中,在更新Android构建工具(安装26.0.3和27.0.3)并删除旧版本(26.0.2)之后,没有显式构建工具版本的任何项目的Gradle同步失败,并且:

Error:Failed to find Build Tools revision 26.0.2
<a href="install.build.tools">Install Build Tools 26.0.2 and sync project</a>
Run Code Online (Sandbox Code Playgroud)

即使是新创建的项目也会发生这种情 据推测,Gradle 3.0.1的Android插件默认使用最新的构建工具版本,但似乎并非如此.

如何让AS在所有项目中默认使用最新版本?

我想使用更新的工具,而不是陷入26.0.2.我可以android.buildToolsVersion在特定项目中使用Gradle属性来构建它们,但这种解决方法不是最理想的.清理项目或使缓存无效并重新启动AS不会产生任何影响.

系统

  • OS X 10.12.6
  • AS 3.0.1,构建AI-171.4443003
    构建工具26.0.3,27.0.3
    插件:
    • Gradle 171.4249.39
  • JRE 1.8.0_152
  • Gradle 4.1
  • Android插件[for Gradle] 3.0.1

android-studio android-gradle-plugin

5
推荐指数
0
解决办法
267
查看次数

如何使pdf文件受密码保护?

我想让pdf文件受密码保护。我只是用谷歌搜索它并找到下面给出的一个很好的解决方案。它工作正常但是在我使用下面给定的代码保护 pdf 后,它清除了我的 pdf 中已经存在的所有数据。

此代码使用的 jar 文件是:

itextpdf-5.2.1.jar

bcmail-jdk16-1.46.jar

bcprov-jdk16-1.46.jar

bctsp-jdk16-1.46.jar

保护 PDF 的代码:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Secure_file {
    private static String USER_PASSWORD = "password";
    private static String OWNER_PASSWORD = "secured";
    public static void main(String[] args) throws IOException {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.pdf"));
            writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
            document.open();
            document.add(new Paragraph("This is Password Protected PDF document."));
            document.close();
            writer.close();
        } catch …
Run Code Online (Sandbox Code Playgroud)

java pdf

4
推荐指数
1
解决办法
2万
查看次数

INSTALL_FAILED_INVALID_APK错误,调试版本代码1与0不一致

我在Android Studio版本2.3.3上构建了一个Android应用程序.当我尝试将应用程序部署到手机(lennovo)或模拟器(GenyMotion)时,我收到一个错误,其中说:

INSTALL_FAILED_INVALID_APK: /data/app/vmdl235652946.tmp/4_yCommerceApp-b_c-dev-debug version code 1 inconsistent with 0.
Run Code Online (Sandbox Code Playgroud)

我在这里查看了各种帖子并尝试了以下方法,但这些方法似乎都没有帮助

  1. 重新启动Android Studio后,是否进行了干净的构建,然后进行了重建

  2. 已断开连接且已重新连接的设备

  3. 我删除了.idea,.gradle并构建目录并重新构建

有什么建议可以解决这个问题吗?这里引用的版本代码是什么?我在AndroidManifest.xml中看到一个版本代码,如下所示:

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">      
Run Code Online (Sandbox Code Playgroud)

我在其他任何地方都没有看到任何其他版本代码.顺便说一下,我没有使用Android模拟器,因为它们似乎需要数小时才能启动.我的处理器是AMD,而不是英特尔.我为仿真器下载了ARM映像.

android android-emulator build.gradle

4
推荐指数
1
解决办法
3743
查看次数

Android Studio 3.0 RC 2

Error:failed linking references.
Error:java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Error:Execution failed for task ':app:processDebugResources'.> Failed to execute aapt

        2017-10-24 08:17:11,957 [se-915-b01]   INFO -     #com.intellij.util.ui.JBUI - UI scale factor: 1.0 
        2017-10-24 08:17:45,060 [entQueue-0]   INFO - llij.ide.plugins.PluginManager - Cannot find optional descriptor duplicates-groovy.xml 
        2017-10-24 08:17:53,301 [entQueue-0]   INFO - llij.ide.plugins.PluginManager - 37 plugins initialized in 21652 ms 
        2017-10-24 08:17:53,310 [entQueue-0]   INFO - llij.ide.plugins.PluginManager - Loaded …
Run Code Online (Sandbox Code Playgroud)

android android-studio

4
推荐指数
1
解决办法
1万
查看次数

更新了 android studio 并通过 Gstreamer 构建失败

使用新的 android gradle 插件更新到 Android Studio 3.0.0。虽然内置项目收到消息:

What went wrong:
Execution failed for task `':app:externalNativeBuildDebug'`.

Expected output file at `gst-build-arm64-v8a/libgstreamer_android.so` for target `gstreamer_android` but there was none
Run Code Online (Sandbox Code Playgroud)

libgstreamer_android.so库文件已经存在。对于本机代码,我使用 ndk-build。有人有这个问题吗?

gstreamer android-studio android-gradle-plugin gradle-plugin

4
推荐指数
1
解决办法
1563
查看次数

在Android模拟器上禁用"保存状态"?

想知道是否有办法关闭Android虚拟设备的新"保存状态..."功能,以便它们立即退出并且不花费几秒钟保留状态.

android emulation android-virtual-device android-studio

3
推荐指数
1
解决办法
2126
查看次数

Android Studio 3.0:无法解析模块依赖性

我有一个主要项目和第二个项目(模块).

在将模块项目作为模块依赖项导入到app项目并尝试同步所有内容后,我收到以下错误:

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :module.
Could not resolve project :module.
Required by: project :app
Unable to find a matching configuration of project :module:
 - Configuration 'debugApiElements':
     - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
     - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found incompatible value 'Apk'.
     - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
     - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
 - Configuration 'debugMetadataElements':
     - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and …
Run Code Online (Sandbox Code Playgroud)

performance android android-layout android-studio android-gradle-plugin

2
推荐指数
1
解决办法
1113
查看次数