How to determine/get correct S Voice packet in And

2019-08-02 21:44发布

问题:

I am using Android to turn on my S Voice application in Android. As previous work, I will use the follows code to turn on it

String SVOICE_PACKAGE_NAME = "com.vlingo.midas";
String SVOICE_LISTEN_ACTION = "com.sec.action.SVOICE";
Intent intent = new Intent();
intent.setPackage(SVOICE_PACKAGE_NAME);
intent.setAction(SVOICE_LISTEN_ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
    getApplication().startActivity(intent);
} catch (final ActivityNotFoundException e) {
    e.printStackTrace();
} catch (final Exception e) {
    e.printStackTrace();
}

The above code worked well in Galaxy S4 with Android 5.0. However, the issue comes from first and second lines in Galaxy S7 with Android 6.0. In Galaxy S7 with Android 6.0, the first and second lines have to modify as

SVOICE_PACKAGE_NAME = "com.samsung.voiceserviceplatform";
SVOICE_LISTEN_ACTION = "com.sec.action.SVOICE";

And also the application name S Voice with changing from "S Voice" to "S Voice App". That changing gives me a difficult work. Hence, I want to determine the S Voice App in my phone before deciding calls these function. Currently, I do not know the changing is from Android version or the device. Could you have any idea to adapt the issue in various phones: S4 and S7?

回答1:

Whenever opening applications, there could be package or application name differences. Here is a standard utility method to check:

/**
 * Check if the user has a package installed
 *
 * @param ctx         the application context
 * @param packageName the application package name
 * @return true if the package is installed
 */
public static boolean isPackageInstalled(@NonNull final Context ctx, @NonNull final String packageName) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "isPackageInstalled");
    }

    try {
        ctx.getApplicationContext().getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    } catch (final PackageManager.NameNotFoundException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "isPackageInstalled: NameNotFoundException");
        }
    } catch (final NullPointerException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "isPackageInstalled: NullPointerException");
        }
    } catch (final Exception e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "isPackageInstalled: Exception");
        }
    }

    return false;
}

You'll need to remove my custom logging.