SearchView为WebView Url / Google搜索栏

时间:2018-01-17 20:43:53

标签: java android webview searchview

我尝试将SearchView用作网址/谷歌搜索。我从开发者网站上读到了该怎么做但我的应用程序不会让我搜索无论如何。目前我正在尝试这种方法。如果它不包含.com / ca /​​ org等,我希望能够搜索谷歌......但是我现在拥有它的方式却不会加载到webView中。

Main.Java

@Override
 public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.options_menu, menu);

    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    assert searchManager != null;
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    Intent searchIntent = getIntent();
    String query = searchIntent.getStringExtra(SearchManager.QUERY);
    if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())) {

        mWebView.loadUrl("https://google.ca/result/?q="+query);

    }

    return super.onCreateOptionsMenu(menu);
}

我现在还不知道还有其他办法。所以我要求帮助, 如果您需要查看更多文件,请询问。

logcat的

  '01-17 15:47:47.653 1583-1620/? E/ActivityManager: Failure starting process com.equiware.mickeyt.unite
                                              java.lang.SecurityException: Package com.equiware.mickeyt.unite is currently frozen!
                                                  at com.android.server.pm.PackageManagerService.checkPackageStartable(PackageManagerService.java:3520)
                                                  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3936)
                                                  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3896)
                                                  at com.android.server.am.ActivityManagerService.startProcessLocked(ActivityManagerService.java:3777)
                                                  at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1440)
                                                  at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:2676)
                                                  at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2183)
                                                  at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:1861)
                                                  at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:1851)
                                                  at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:5405)
                                                  at com.android.server.am.ActivityManagerService.removeProcessLocked(ActivityManagerService.java:6806)
                                                  at com.android.server.am.ActivityManagerService.killPackageProcessesLocked(ActivityManagerService.java:6466)
                                                  at com.android.server.am.ActivityManagerService.forceStopPackageLocked(ActivityManagerService.java:6606)
                                                  at com.android.server.am.ActivityManagerService$MainHandler.handleMessage(ActivityManagerService.java:2059)
                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                  at android.os.Looper.loop(Looper.java:154)
                                                  at android.os.HandlerThread.run(HandlerThread.java:61)
                                                  at com.android.server.ServiceThread.run(ServiceThread.java:46)
01-17 15:47:47.767 1583-1622/? E/libprocessgroup: failed to kill 1 processes for processgroup 29664
01-17 15:47:47.909 29504-29504/? E/PhoneWindow: Circular progress bar not located in current window decor
01-17 15:47:48.052 1583-2282/? E/OverlayManager: Cannot found overlays for target: com.equiware.mickeyt.unite
01-17 15:47:48.052 1583-2282/? E/OverlayManager: Cannot found overlays for target: com.equiware.mickeyt.unite
01-17 15:47:48.057 2572-2572/? E/FMFRW_LgFmService: The Service for Chip Vendor is not ready
01-17 15:47:48.067 2321-2321/? E/SystemServicesProxy: recent task thumbnail from AM is null, and descriptor is null
01-17 15:47:48.104 2945-2945/? E/LCardEmulationManager: initDefaultRoutePath() - route : 243
01-17 15:47:48.145 1583-2282/? E/OverlayManager: Cannot found overlays for target: com.equiware.mickeyt.unite
01-17 15:47:48.159 2321-2423/? E/KeyguardModel: package:  not found!
01-17 15:47:48.159 2321-2423/? E/KeyguardModel: package:  not found!
01-17 15:47:48.161 2321-2423/? E/KeyguardModel: package:  not found!
01-17 15:47:48.310 2321-2321/? E/ActivityThread: Failed to find. provider info for com.lge.lgpay.lockscreen.shortcut
01-17 15:47:48.310 2321-2321/? E/com.lge.lockscreen.widget.draglayer.AbsUnlockHandler: Failed to show the LG Quick Pay shortcut : LG Pay is not exist or an exception has occured.
01-17 15:47:48.582 30170-30170/? E/asset: setgid: Operation not permitted
01-17 15:47:48.599 30174-30174/? E/asset: setgid: Operation not permitted
01-17 15:47:48.737 2885-11015/? E/NetworkScheduler.SR: Invalid parameter app
01-17 15:47:48.737 2885-11015/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
s01-17 15:47:48.768 4100-4696/? E/Drive.UninstallOperation: Package still installed com.equiware.mickeyt.unite
01-17 15:47:48.987 30178-30256/? E/libEGL: validate_display:99 error 3008 (EGL_BAD_DISPLAY)
01-17 15:47:49.029 30178-30178/? E/chromium: [ERROR:interface_registry.cc(104)] Failed to locate a binder for interface: autofill::mojom::PasswordManagerDriver
 01-17 15:47:49.143 29812-29812/? E/Finsky: [1] com.google.android.finsky.wear.bp.a(847): onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
  01-17 15:47:49.235 1583-2122/? E/WifiScanner: listener cannot be found
 01-17 15:47:49.552 3512-3512/? E/WidgetViewCreator: setBgColorForSmartBulletin called
 01-17 15:47:49.553 3512-3512/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin
 01-17 15:47:50.226 2885-14359/? E/NetworkScheduler.SR: Invalid parameter app
01-17 15:47:50.226 2885-14359/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
01-17 15:47:51.010 3512-3512/? E/WidgetViewCreator: setBgColorForSmartBulletin called
01-17 15:47:51.010 3512-3512/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin
01-17 15:47:51.022 3512-3512/? E/WidgetViewCreator: setBgColorForSmartBulletin called
01-17 15:47:51.023 3512-3512/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin
 01-17 15:47:51.048 3512-3512/? E/WidgetViewCreator: setBgColorForSmartBulletin called
01-17 15:47:51.049 3512-3512/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin
01-17 15:47:51.090 30459-30459/? E/LocationManager: [LGNSS] Disable_PrivacyLocation_Information [tOperator : RGS ] , [privacy_check : NA]
 01-17 15:47:51.102 1583-3083/? E/BatteryStatsService: no controller energy info supplied
01-17 15:47:51.102 1583-3083/? E/BatteryStatsService: no controller energy info supplied
 01-17 15:47:51.126 1583-3083/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
 01-17 15:47:51.170 30459-30459/? E/BatteryUtils:  wifiPowerPerPacket :2.7306666666666667E-4 mobilePowerPerms : 5.1441666666666666E-5 mobilePowerPerPacket 1.1705837037037035E-6
01-17 15:47:52.697 851-1023/? E/ThermalEngine: TM Id 'LCD_ON_MONITOR' Sensor 'lcd-brightness' - alarm  raised 1 at 255.0 degC
01-17 15:48:00.141 3512-3512/? E/WidgetViewCreator: setBgColorForSmartBulletin called
01-17 15:48:00.144 3512-3512/? E/ActivityThread: Failed to find provider info for com.lge.launcher2.smartbulletin
01-17 15:48:04.227 30527-30527/? E/Pref: get_inner_onoff: false
01-17 15:48:04.233 30527-30527/? E/MYPLACE_Utils: activityFinish
01-17 15:48:04.236 30527-30527/? E/MYPLACE_Receiver: onReceive com.lge.myplace.myplace_settings_search_intent
01-17 15:48:04.236 30527-30527/? E/MYPLACE_Receiver: INTENT_MYPLACE_SETTINGS_SEARCH false
01-17 15:48:04.239 30527-30527/? E/Pref: get_onoff: 0
 01-17 15:48:04.489 30515-30515/? E/Finsky: [1] com.google.android.finsky.wear.bp.a(847): onConnectionFailed:     ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}'

1 个答案:

答案 0 :(得分:0)

尝试此代码,我实现了this site中的方法。

我的 WebViewActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.webview_menu, menu);
    if(menu instanceof MenuBuilder){

        MenuBuilder menuBuilder = (MenuBuilder) menu;
        menuBuilder.setOptionalIconsVisible(true);
    }

    MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            searchViewAndroidActionBar.clearFocus();
//Called when the user submits the query. This could be due to a key press on the keyboard or due to pressing a submit button.
            webView.loadUrl("https://www.google.co.id/search?q="+query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
//Called when the query text is changed by the user.
            return false;
        }
    });

    // Configure the search info and add any event listeners...
    return super.onCreateOptionsMenu(menu);
}

对于您的情况,只需替换您的网址字符串

  

https://google.ca/result/?q=

  

https://www.google.ca/search?q=

然后我的 webview_menu.xml

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_search_white_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView" />

文档为right here