2011-06-21 50 views
4

我似乎遇到了一些問題讓我的應用程序去......哎呀安卓:搜索意圖不工作

我有這個問題在目前是,我有一個活動上發佈我的應用程序是是一個可搜索的設施(通過Android快速搜索)。搜索功能起作用並啓動我的SearchActivity類,其中顯示的結果很簡單。

但是,當我嘗試從結果活動(SearchActivity)搜索時,搜索框彈出並接受放入,但意圖永遠不會被SearchActivity接收或它永遠不會啓動SearchActivity ...我不確定。

這裏是SearchActivity類(本使用相同的OnClickListener作爲工人階級):

public class SearchActivity extends MapActivity implements OnGestureListener, OnDoubleTapListener { 
    public boolean touched = false; 
    private MapView mapView; 
    private MapController mapController; 
    private PlacesItemizedOverlay placesItemizedOverlay; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.main); 
     mapView = (MapView) findViewById(R.id.mapView); 

     Button searchRequest = (Button)findViewById(R.id.searchRequest); 

     searchRequest.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      onSearchRequested(); 
      } 
     }); 

    mapView.getController(); 
    handleIntent(getIntent()); 
    } 

    @Override 
    public boolean onSearchRequested() { 
     return super.onSearchRequested(); 
    } 

    private void handleIntent(Intent intent) { 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      // handles a search query 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      mapFromQuery(query); 
     } 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
    } 

    @Override 
    public boolean onDown(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { 
     // TODO Auto-generated method stub 
    return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onDoubleTap(MotionEvent arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

水龍頭,我不相信有什麼影響的名單,但我列入他們只是櫃面。這裏是我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.test.android.example" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <uses-library android:name="com.google.android.maps" /> 

     <activity android:name=".SplashScreen" 
        android:label="@string/app_name"> 

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

     <activity android:name=".Main"> 
      <intent-filter> 
       <action android:name="com.test.android.example.Main" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
      <meta-data android:name="android.app.default_searchable" 
         android:value=".SearchActivity" /> 
     </activity> 

     <activity android:name=".SearchActivity" 
       android:launchMode="singleTop" > 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data android:name="android.app.searchable" 
         android:resource="@layout/searchable"/> 
     </activity> 
    </application> 
</manifest> 

請幫助:(

+0

你好,我可以看到你的mapFromQuery函數看起來像 – CQM

回答

11

終於找到了解決辦法!

所有需要添加的是:

@Override 
public void onNewIntent(Intent intent) { 
    super.onNewIntent(intent);  
    setIntent(intent); 
    handleIntent(intent); 
} 

就是這樣!打賭別人正在尋找相同的結果,享受!:)

+0

感謝從一個小時卡住了 – Ravi

0

爲什麼不使用正常的意圖與演員像這樣:

Intent intent = new Intent(); 
intent.setClassName("yourpackage","yourpackage.youractivity"); 
intent.putExtra("Var name", yourVar); //yourVar can be any type, int, string, etc... 
startActivity(intent); 
+1

在這個例子中,我使用快速搜索框,它啓動android.intent.action.SEARCH onSearchRequested () 叫做。我不認爲有辦法編輯這個意圖,你只能引導它。感謝您的建議:) – Paul

+0

噢!對不起, – CodeKrieger