2016-07-05 16 views
6

Tôi đang viết trình khởi chạy và muốn có thể mở tìm kiếm dưới dạng lớp phủ thay vì toàn màn hình trong Ứng dụng Google.Mở tìm kiếm toàn cầu dưới dạng lớp phủ

Cho đến nay tôi chỉ tìm thấy một cách để mở tìm kiếm trong Google App toàn màn hình như sau (lấy từ mã nguồn AOSP Launcher3):

public static boolean openSearch(Context context) { 

     SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); 
     ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity(); 
     if (globalSearchActivity == null) { 
      Timber.w("No global search activity found."); 
      return false; 
     } 
     Intent intent = new Intent(android.app.SearchManager.INTENT_ACTION_GLOBAL_SEARCH); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.setComponent(globalSearchActivity); 
     Bundle appSearchData = new Bundle(); 
     appSearchData.putString("source", "launcher-search"); 

     intent.putExtra(android.app.SearchManager.APP_DATA, appSearchData); 

     intent.putExtra(android.app.SearchManager.QUERY, ""); 
     intent.putExtra(android.app.SearchManager.EXTRA_SELECT_QUERY, true); 
     try { 
      context.startActivity(intent); 
      return true; 
     } catch (ActivityNotFoundException ex) { 
      Timber.w("Global search activity not found: %s", globalSearchActivity); 
      return false; 
     } 

    } 

Tôi biết nó có thể vì bệ phóng khác như Nova và Action Launcher được quản lý để làm điều đó ...

Trả lời

4

Đã phát hiện ra ...

public static boolean showGlobalSearchOverlay(Context context) { 
    ComponentName globalSearchActivity = 
      new ComponentName("com.google.android.googlequicksearchbox", 
        "com.google.android.apps.gsa.queryentry.QueryEntryActivity"); 

    Intent intent = new Intent(Intent.ACTION_MAIN); 
    intent.setComponent(globalSearchActivity); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    try { 
     context.startActivity(intent); 
     return true; 
    } catch (Throwable e) { 
     Timber.w("Unable to show search overlay"); 
     return false; 
    } 
} 
Các vấn đề liên quan