手机上使用google 搜索引擎
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.antking.search" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SearchActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <--!这里加上--> <intent-filter> <action android:name="android.intent.action.WEB_SEARCH"></action> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> </activity> </application> <--!这里加上--> <meta-date android:name="android.app.default_searchable" android:value=".SearchActivity"/> </manifest>
package com.android.antking.search;
import android.app.Activity; import android.app.SearchManager; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class SearchActivity extends Activity { //定义私有整形变量 private final int MENU =1; //声明Intent对象 private Intent intent; //定义键盘模式为按键搜索模式 private final int KeyMode =Activity.DEFAULT_KEYS_SEARCH_LOCAL; //使用Android自定义图标 private final int icon = android.R.drawable.ic_menu_search; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try{ setContentView(R.layout.main); //设置键盘模式,按键盘弹出搜索狂 setDefaultKeyMode(KeyMode); //获取intent intent = getIntent(); searchWeb(intent); }catch(Exception e){ Toast.makeText(SearchActivity.this, "一场错误"+e.toString(), Toast.LENGTH_SHORT).show(); } } /*searchWeb 根据意图行为为键盘算是否进行Web搜索*/ private void searchWeb(Intent intent){ //获取意图的行为 String action = intent.getAction(); //若意图的行为是Web搜索,则搜索 if(Intent.ACTION_WEB_SEARCH.equals(action)){ searchHandle(intent); } } protected void onNewIntent(Intent intent){ setIntent(intent); searchWeb(intent); } /*searchHandle()获取SearchManager的查询*/ private void searchHandle(Intent intent){ final String query = intent.getStringExtra(SearchManager.QUERY); Toast.makeText(this, query, Toast.LENGTH_SHORT).show(); } public boolean onCreateOptionsMenu(Menu menu){ boolean result = super.onCreateOptionsMenu(menu); //添加菜单选项 MenuItem menuItem = menu.add(0,MENU,0,"nihao"); //设置图标 menuItem.setIcon(icon); return result; } public boolean onMenuItemSelected(int featureId,MenuItem item){ switch(item.getItemId()){ case MENU: onSearchRequested(); return true; default: return super.onMenuItemSelected(featureId, item); } } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28