Launch google quick search box

Here's a little helper function to launch the Google quick search box in android ;)
NOTE: This may stop working in a future update to the Google app, be sure to test it out first.
/**
 * Launch the Google quick search box overlay activity, otherwise, fall back to
 * launching the device default assist app.
 */
private void launchQuickSearch(Context context) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    try {
        // Explicit intent to launch the google quick search box
        intent.setClassName("com.google.android.googlequicksearchbox",
                "com.google.android.search.queryentry.QueryEntryActivity");

        // If there's no associated activity, then try launching any other available app
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            intent.setAction(Intent.ACTION_ASSIST);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            }
        }
    } catch (ActivityNotFoundException e) {
        Log.e(TAG, "Google search activity not found");
        Log.i(TAG, "Trying to launch default assist app.");
        intent = new Intent(Intent.ACTION_ASSIST);
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }
}


@Override
public void onClick(View v) {
    launchQuickSearch(MainActivity.this);
    // launchQuickSearch(v.getContext());
}

No comments:

Post a Comment

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...