开发者

Android: Search Intent not working

I seem to be running into a number of problems getting my app going ... grrr

The issue I have at the moment is, I have an Activity on launch of my app that is a searchable facility (via android quick search). The search function works and launches my SearchActivity class where the results as displayed, simples.

BUT, when I attempt a search from the results activity (SearchActivity), the search box pops up and accepts in put, but the intent is never received by SearchActivity or it never launches SearchActivity ... I'm not sure.

Here is the SearchActivity class (this uses the same OnClickListener as the working class):

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, MotionEve开发者_StackOverflownt 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;
    }
}  

The list of taps I don't believe are affecting anything, but I included them just incase. Here is my manifest file:

<?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>

Please help :(


Finally found the solution!!

All that needed to be added was:

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

Thats it! Bet someone else is looking for the same result, enjoy!:)


Why not use normal intents with extras? Like so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜