Android:搜索意图不起作用

发布于 11-16 10:18 字数 5024 浏览 2 评论 0原文

我似乎在让我的应用程序运行时遇到了许多问题... grrr

我目前遇到的问题是,我在启动我的应用程序时有一个活动,它是一个可搜索的工具(通过 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>

请帮助:(

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, 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;
    }
}  

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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠2024-11-23 10:18:12

终于找到解决办法了!!

需要补充的只是:

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

就是这样!打赌其他人也在寻找相同的结果,享受吧!:)

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!:)

吾性傲以野2024-11-23 10:18:12

为什么不将普通意图与附加功能一起使用呢?就像这样:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文