我在我的应用程序中创建了一个网格视图,现在我想在其中包含语音识别,以便: 当用户说“home”时,主页图标将会打开:
我是Android开发的初学者,所以请给我一个详细的答案:
我创建了一个voice.java类,并使用了识别器意图,ACTION_RECOGNIZER_SPEECH以及onactivityResult()方法,我无法将它与我的图标链接起来。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
//import android.content.ClipData.Item;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
GridView gridview;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.home);
Bitmap facultyIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.faculty);
Bitmap courcesIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.cources);
Bitmap admissionIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.admission);
Bitmap newsIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.news);
Bitmap acheivmentsIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.award);
Bitmap cornerIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.corner);
Bitmap amenitiesIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.amenities);
Bitmap societiesIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.socities);
Bitmap timeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.time_table);
Bitmap eventsIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.events);
Bitmap contactIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.contact);
gridArray.add(new Item(homeIcon, "Home","http://bit.ly.com/"));
gridArray.add(new Item(courcesIcon, "Courses","http://bit.ly.com/courses.html"));
gridArray.add(new Item(facultyIcon, "Faculty","http://bit.ly.com/faculty.html"));
gridArray.add(new Item(admissionIcon, "Admission","http://bit.ly.com/admission.html"));
gridArray.add(new Item(newsIcon, "News","http://bit.ly.com/news.html"));
gridArray.add(new Item(acheivmentsIcon, "Acheivments","http://bit.ly.com/achievement.html"));
gridArray.add(new Item(cornerIcon, "Student Corner","http://bit.ly.com/student-corner.html"));
gridArray.add(new Item(amenitiesIcon, "Amenities","https://www.google.co.in/"));
gridArray.add(new Item(societiesIcon, "Societies","http://bit.ly.com/socities.html"));
gridArray.add(new Item(timeIcon, "Time Table","http://bit.ly.com/time-tables.html"));
gridArray.add(new Item(eventsIcon, "Events","https://www.google.co.in/"));
gridArray.add(new Item(contactIcon, "Contact Us","http://bit.ly.com/directories.html"));
gridview = (GridView) findViewById(R.id.gridView);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridview.setAdapter(customGridAdapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String url=gridArray.get(arg2).getWebUrl();
Intent intent = new Intent("com.//package name.SECONDACTIVITY");
intent.putExtra("url", url);
startActivity(intent);
}
});
}
}
}