1

我試圖在用戶單擊搜索建議時設置Intent.ACTION_VIEW。它仍然執行ACTION_SEARCH的意圖,因此它會在建議框中搜索提供的信息,但我希望它將它傳遞給已顯示單擊對象的Activity。我不使用SearchActivity及其方法(onNewIntent等),我只使用Activity,它顯示結果列表 - 其設置爲_default_searchable。Intent.ACTION_VIEW在搜索中不起作用

但是我應該怎麼做,如果我需要將它傳遞給不同的活動,當建議項目被選中?

我在清單中設置android:searchSuggestIntentAction="android.intent.action.VIEW"和意圖過濾器在第二活動intent.VIEW但它不工作

你有任何想法,該怎麼做?

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
     android:label="@string/app_name" 
     android:hint="@string/search_hint" 
     android:searchSuggestAuthority="com.example.animalist.SuggestionProvider" 
     android:searchSuggestSelection=" ?" 
     android:searchSuggestIntentAction="android.intent.action.VIEW" 


     /> 

清單

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
       <provider 
        android:authorities="com.example.animalist.SuggestionProvider" 
        android:name=".SuggestionProvider" > 
       </provider> 
     <meta-data android:name="android.app.default_searchable" 
        android:value=".SearchActivity"/> 
     <activity 
      android:name="com.example.animalist.MainActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleTop" 
      android:screenOrientation="portrait" > 
      <meta-data android:name="android.app.default_searchable" 
         android:value=".SearchActivity"/> 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </activity> 

     <activity 
      android:name="com.example.animalist.MoreActivity" 
      android:label="@string/title_activity_more" 
      android:launchMode="singleTop" 
      android:parentActivityName="com.example.animalist.MainActivity" 
      android:screenOrientation="portrait" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.animalist.MainActivity" /> 

      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
      </intent-filter> 


     </activity> 

     <activity 
      android:name="com.example.animalist.SearchActivity" 
      android:label="@string/title_activity_search" 
      android:launchMode="singleTop" 
      android:parentActivityName="com.example.animalist.MainActivity" 
      android:screenOrientation="portrait" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.animalist.MainActivity" /> 


      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.app.searchable" 
       android:resource="@xml/searchable" /> 

     </activity> 

    </application> 

提供商

public class SuggestionProvider extends SearchRecentSuggestionsProvider { 
    private static final String TAG = "SuggestionProvider"; 

    private static final int SEARCH_SUGGESTIONS = 1; 

    private static final UriMatcher sURLMatcher = new UriMatcher(
      UriMatcher.NO_MATCH); 

    static { 
     sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY, 
       SEARCH_SUGGESTIONS); 
     sURLMatcher.addURI("*", SearchManager.SUGGEST_URI_PATH_QUERY + "/*", 
       SEARCH_SUGGESTIONS); 
    } 

    private static final String[] COLUMNS = new String[] { 
      "_id", 
      SearchManager.SUGGEST_COLUMN_TEXT_1, 
      SearchManager.SUGGEST_COLUMN_INTENT_ACTION, 
      SearchManager.SUGGEST_COLUMN_QUERY, 
      SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA 
    }; 

    public SuggestionProvider() { 
    } 

    @Override 
    public boolean onCreate() { 
     return true; 
    } 

    @Override 
    public Cursor query(Uri url, String[] projectionIn, String selection, 
      String[] selectionArgs, String sort) { 
     int match = sURLMatcher.match(url); 
     switch (match) { 
      case SEARCH_SUGGESTIONS: 
       String query = url.getLastPathSegment(); 
       MatrixCursor cursor = new MatrixCursor(COLUMNS); 

       ParseQuery<Animal> squery = ParseQuery.getQuery(Animal.class); 
      try { 

       List<Animal> results = squery.find(); 
       for (Animal animal : results) { 
         addRow(cursor, animal.getAnimal()); 
      } }catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      return cursor;} 
     return null; 
       } 





    private void addRow(MatrixCursor cursor, String string) { 
     long id = cursor.getCount(); 
     cursor .newRow().add(id).add(string).add(Intent.ACTION_SEARCH).add(string); 
    } 

    @Override 
    public String getType(Uri url) { 
     int match = sURLMatcher.match(url); 
     switch (match) { 
      case SEARCH_SUGGESTIONS: 
       return SearchManager.SUGGEST_MIME_TYPE; 
      default: 
       throw new IllegalArgumentException("Unknown URL: " + url); 
     } 
    } 

    @Override 
    public int update(Uri url, ContentValues values, String where, String[] whereArgs) { 
     throw new UnsupportedOperationException("update not supported"); 
    } 

    @Override 
    public Uri insert(Uri url, ContentValues initialValues) { 
     throw new UnsupportedOperationException("insert not supported"); 
    } 

    @Override 
    public int delete(Uri url, String where, String[] whereArgs) { 
     throw new UnsupportedOperationException("delete not supported"); 
    } 
} 

提前感謝所有幫助

回答

1

雖然它是不完全的一個很好的方式如果,我已經通過製作一個透明的活動來處理VIEW和SEARCH的意圖,從而解決了這個問題。

通過此活動,您可以啓動要處理這兩種情況並完成透明活動並傳遞搜索參數的活動。

+0

感謝您的回答。所以這個活動不是可見的,它只處理意圖? – marson

+0

是的。我無法找到讓搜索建議指向每個意圖的活動的方法。另一種選擇是爲VIEW和SEARCH加載一個不同的片段,因此你不需要創建透明的活動。 – daentech