Integrate the Android SDK

更新时间:
复制 MD 格式

1. Download the SDK

You do not need to download the SDK if you integrate it from the Maven repository. For more information, see section 2.1 Quick integration with the Maven repository (remote synchronization).

To integrate the SDK manually, go to the EMAS console and download the SDK.

2. Preparations

2.1 Android Studio development

1. Quick integration with the Maven repository (remote synchronization) (Recommended)

Configure the Maven repository URL in the build.gradle file in the root directory of your project:

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.aliyun.com/nexus/content/repositories/releases/'
        }
    }
}

Add the dependency to the build.gradle file in the corresponding module:

dependencies {
    ......
    compile 'com.aliyun.ams:alicloud-android-feedback:3.4.2'
    ......
}
Important

If the compilation fails, add `android:allowBackup="false"` and `android.enableJetifier=true` to the gradle.properties file.

2. Manual integration

Copy the .aar and .jar files from the downloaded SDK folder to the libs directory. Then, add the following configuration to the build.gradle file:

repositories {
    flatDir {
        dirs 'libs'
    }
}

Add local dependencies:

dependencies {
    implementation(name: 'alicloud-android-feedback-3.4.2', ext: 'aar')
    implementation(name: 'alicloud-android-rest-1.7.3-open-release', ext: 'aar')
    implementation(name: 'alicloud-android-sender-1.1.5', ext: 'aar')
    implementation(name: 'alicloud-android-logger-1.2.0', ext: 'aar')
    implementation files('libs/alicloud-android-utdid-2.6.0.jar')
    implementation files('libs/alicloud-android-beacon-1.0.7.jar')
    implementation files('libs/alicloud-android-crashdefend-0.0.6.jar')
  	implementation files('libs/alicloud-android-tool-1.0.1.jar')
    // Add the dependency on support-v4.
}
Important

Make sure to add the dependency on support-v4.

If you encounter a UTDID conflict, see UTDID Conflict Solution for Alibaba Cloud Mobile Product SDKs.

2.2 Permissions

  • The SDK requires permissions for the album, camera, and audio recording.

  • For Android 6.0 and later, you must implement the permission requests. For earlier versions, declaring the permissions is sufficient.

  • If a user performs an operation that requires a permission that has not been granted, the SDK directly requests the permission from the user. Your application can register a callback listener to display a prompt that explains the purpose of the permission before the SDK requests it.

        // Listen for the permission request for the camera operation.
        FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_CAMERA, new IPermissionRequestInterrupt() {
            @Override
            public void interrupt(Context context, String action, String[] permissions, InterruptCallback callback) {
                showDialog(context,"Camera", "to take a photo for feedback", callback);
            }
        });
        // Listen for the permission request for the album read operation.
        FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_ALBUM, new IPermissionRequestInterrupt() {
            @Override
            public void interrupt(Context context, String action, String[] permissions, InterruptCallback callback) {
                showDialog(context, "Album", "to select a photo for feedback", callback);
            }
        });
        // Listen for the permission request for the audio recording operation.
        FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_AUDIO, new IPermissionRequestInterrupt() {
            @Override
            public void interrupt(Context context, String action, String[] permissions, InterruptCallback callback) {
                showDialog(context, "Audio Recording", "to record an audio description for feedback", callback);
            }
        });

    // This is sample code. Replace it with the permission prompt style of your application.
    private void showDialog(Context context, String permission, String message, final InterruptCallback callback) {
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
        normalDialog.setTitle("Sensitive Permission Authorization");
        normalDialog.setMessage("The " + permission + " permission is required " + message);
        normalDialog.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // The user agrees to grant the permission. Proceed with the request.
                        callback.goOnRequest();
                    }
                });
        normalDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // The user denies the permission. Stop the request.
                        callback.stopRequest();
                    }
                });
        // Show the dialog.
        normalDialog.show();
    }

2.3 Obfuscation configuration

When you package the application with obfuscation, add the following configurations:

-keep class com.taobao.** {*;}
-keep class com.alibaba.** {*;}
-dontwarn com.taobao.**
-dontwarn com.alibaba.**
-keep class com.ut.** {*;}
-dontwarn com.ut.**
-keep class com.ta.** {*;}
-dontwarn com.ta.**

3. Get started

3.1 Initialize the feedback component

FeedbackAPI.init(application, appkey,appSecret);

Parameters:

  • application: The Application object.

  • appkey: The unique identifier for your application on Alibaba Cloud. You can obtain it from the Alibaba Cloud console.

  • appSecret: The appSecret for your application on Alibaba Cloud. You can obtain it from the Alibaba Cloud console.

Important

  • To prevent the appkey, appsecret, or data generated during app runtime from being leaked in logs, disable SDK debugging logs for the online version.

  • Because all users integrate a unified SDK, you must set the appkey and appsecret parameters in your code. These parameters are essential for metering and billing. To prevent information leakage from malicious decompilation, we recommend that you enable obfuscation and reinforce your app before you publish it.

3.2 Open the user feedback interface

To open the user feedback HTML5 interface, start a new Activity:

FeedbackAPI.openFeedbackActivity();

To retrieve the call result, for example, to display a loading indicator:

FeedbackAPI.openFeedbackActivity(final Callable success, final Callable fail)

To open the interface as a Fragment:

 FragmentManager fm = getSupportFragmentManager();
 final FragmentTransaction transaction = fm.beginTransaction();
 final Fragment feedback = FeedbackAPI.getFeedbackFragment();
 // must be called
 FeedbackAPI.setFeedbackFragment(new Callable() {
      @Override
            public Object call() throws Exception {
                transaction.replace(R.id.content, feedback);
                transaction.commit();
                return null;
            }
 }/*success callback*/, null/*fail callback*/);
Note

When you use a fragment, you must use the setFeedbackFragment callback to declare the fragment in the XML file. Activities that wrap fragments are not supported. Make sure to call FeedbackAPI.cleanFeedbackFragment(). The Fragment is from the support-v4 package.

3.3 Get the number of unread feedback messages

FeedbackAPI.getFeedbackUnreadCount(IUnreadCountCallback callback)

The `count` parameter in the onSuccess(int count) callback method specifies the current number of unread messages.

3.4 Set the font size of "History" in the title bar

FeedbackAPI.setHistoryTextSize(20); // The unit is sp.
Note

In version 3.1.0, you must set the font size of History to 0 in the console for this method to take effect.

3.5 Set the image for the back button

FeedbackAPI.setBackIcon(R.drawable.back);

3.6 Immersive taskbar

The immersive taskbar is enabled by default. To disable it, call FeedbackAPI.setTranslucent(false).

Note

In version 3.1.0, you must set the immersive taskbar to true in the console for this method to take effect.

3.7 Set the default contact information

FeedbackAPI.setDefaultUserContactInfo(“1300000XXXX”);

3.8 User nickname

  • After you set a user nickname, it is included in the user's feedback. You can view the nickname in the feedback session in the console.

  • This method was added in v3.1.7.

FeedbackAPI.setUserNick("xxx");

3.9 Voice feedback

This feature requires SDK v3.1.0 or later. In the console, navigate to System Configuration > Mobile Client Configuration > Global Configuration and enable Enable Voice.

3.10 Set custom parameters for feedback messages

JSONObject extInfo = new JSONObject();
extInfo.put("key", "value");
FeedbackAPI.setAppExtInfo(extInfo);

4. Demo

Demo

Note

In the demo project, FeedbackAPI.init is called multiple times to update configurations in real time. In your actual application, call the FeedbackAPI.init method only once in the Application class.