设置自动填充服务列表中未显示自动填充服务

时间:2018-10-24 12:45:12

标签: android

我正在尝试开发一个简单的应用程序,该应用程序使用Android Oreo中的自动填充框架在其他应用程序中自动填充电子邮件。我已经按照本教程开发了该应用程序。但是运行该应用程序后,无法在启动该设置的自动填充服务列表中看到我的自动填充服务。我不知道为什么,请帮忙!

这是构建自动填充服务的代码:

我的服务:

public class EmailAddFiller extends AutofillService {
@Override
public void onFillRequest( @NonNull FillRequest request, @NonNull CancellationSignal cancellationSignal, FillCallback callback) {
    List<AssistStructure.ViewNode> emailFields = new ArrayList<>();
    // Get the structure from the request
    List<FillContext> context = request.getFillContexts();
    AssistStructure structure = context.get(context.size() - 1).getStructure();
    identifyEmailFields(structure
            .getWindowNodeAt(0)
            .getRootViewNode(), emailFields);
    AssistStructure.ViewNode emailField = emailFields.get(0);
    SharedPreferences sharedPreferences =
            getSharedPreferences("EMAIL_STORAGE", MODE_PRIVATE);

    String primaryEmail =
            sharedPreferences.getString("PRIMARY_EMAIL", "");
    String secondaryEmail =
            sharedPreferences.getString("SECONDARY_EMAIL", "");
    Dataset primaryEmailDataSet =
            new Dataset.Builder()
                    .setValue(
                            emailField.getAutofillId(),
                            AutofillValue.forText(primaryEmail)
                    ).build();

    Dataset secondaryEmailDataSet =
            new Dataset.Builder()
                    .setValue(
                            emailField.getAutofillId(),
                            AutofillValue.forText(secondaryEmail)
                    ).build();
    FillResponse response = new FillResponse.Builder()
            .addDataset(primaryEmailDataSet)
            .addDataset(secondaryEmailDataSet)
            .build();

    callback.onSuccess(response);
}

private void identifyEmailFields(AssistStructure.ViewNode node, List<AssistStructure.ViewNode> emailFields) {
    if(node.getClassName().contains("EditText")) {
        String viewId = node.getIdEntry();
        if(viewId!=null && (viewId.contains("email")
                || viewId.contains("username"))) {
            emailFields.add(node);
            return;
        }
    }
    for(int i=0; i<node.getChildCount();i++) {
        identifyEmailFields(node.getChildAt(i), emailFields);
    }
}

@Override
public void onSaveRequest(@NonNull SaveRequest request, @NonNull SaveCallback callback) {

}

}

以及Manifestation文件中的配置:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".EmailAddFiller"
        android:label="My Autofill Service"
        android:permission="android.permission.BIND_AUTO_FILL">
        <meta-data android:name="android.autofill"
            android:resource="@xml/email_address_filler"/>
        <intent-filter>
            <action android:name="android.service.autofill.AutoFillService"/>
        </intent-filter>
    </service>
</application>

并构建gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.tam.autofillapp"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.jetbrains:annotations-java5:15.0'
    implementation 'com.android.support:design:28.+'

}

我想念什么吗?如果还有关于该主题的其他示例,我们将不胜感激。非常感谢!

0 个答案:

没有答案