Android 5.0 Lollipop Hotword detection API - Java for Android App Development

I have a problem with the new Android API21, specifically the VoiceInteractorService.
I would like to use the new Hotword detection in API21. If i press a button, a hotword detection for the word 'computer' should start. The new API should provide this functionality.
When I have an Activity (MainActivity) and I want to call the createAlwaysOnHotwordDetector(String keyphrase, Locale locale, AlwaysOnHotwordDetector.Callback callback) method from the VoiceInteractorService, I get an error: java.lang.IllegalStateException: Not available until onReady() is called. I tried to solve this temporarily by using a while with try catch loop to see when onReady() is called and I can excecute the createAlwaysOnHotwordDetector() method. I found out that onReady() is never called, even after letting the system loop for 15 minutes.
It seems that the VoiceInteractionService isn't able to create an HotwordDetector.
Does anybody have an idea how to solve this problem?
Thanks in advance.
This is my Activity for calling the VoiceInteractorService.
Code:
public class MainActivity extends Activity {
Button btn;
VoiceInteractionService service;
AlwaysOnHotwordDetector.Callback callback;
Locale locale = new Locale("nl-NL");
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
ctx = getApplicationContext();
service = new VoiceInteractionService();
btn.setOnClickListener(new OnClickListener() {
// Create Hotword detector on button click
@Override
public void onClick(View v) {
service.createAlwaysOnHotwordDetector("computer", locale, callback);
}
});
callback = new Callback() {
@Override
public void onRecognitionResumed() {
// TODO Auto-generated method stub
}
@Override
public void onRecognitionPaused() {
// TODO Auto-generated method stub
}
@Override
public void onError() {
// TODO Auto-generated method stub
Log.d("error", "error");
}
@Override
public void onDetected(EventPayload eventPayload) {
// TODO Auto-generated method stub
// Display Toast message when Hotword is detected
Toast.makeText(ctx, "Hotword detected", Toast.LENGTH_LONG).show();
}
@Override
public void onAvailabilityChanged(int status) {
// TODO Auto-generated method stub
}
};
}
}

I think you need to extend your own voice interaction service before trying to create a hot word detector

I have extended this voice interaction service and called createAlwaysOnHotwordDetector in onReady function as per android developer portal guidelines. However the onReady function never got called. If you have tried this, can you please tell us how this can be made.

Related

My Soundboard force closes every now and again

I have a problem: Everytime I click a button on my soundboard it has a 50/50 chance of force closing. the only thing I can think of is something in this line of code maybe:
Code:
public class MyMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
final MediaPlayer pButtonClick = MediaPlayer.create(this, R.raw.pants);
Button bpants = (Button) findViewById(R.id.pants);
bpants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pButtonClick.start();
}
});
...If you wish to later replay the media, then you must reset() and prepare() the MediaPlayer object before calling start() again. (create() calls prepare() the first time.)....
Click to expand...
Click to collapse
Thats probably the problem.
http://developer.android.com/guide/topics/media/index.html

[Q] Schedule with long delay

Hello guys. I'm making an app, its a kind of alarm scheduler but I have a problem with the code. Thats what im using
Code:
ScheduledThreadPoolExecutor scheduler=new ScheduledThreadPoolExecutor(1);
scheduler.schedule(new Runnable() {
public void run(){
alarm(1);
} }, delay , TimeUnit.SECONDS);
It works for low values of "delay" if I put a high value (11000 for example) and leave the device alone, I think it enters in sleep mode and dont execute the timer.
How can I solve that?
Thanks in advance
I dont know about the scheduler but an alarm manager can wake the phone from sleep if you want. Or run the alarm in the background while asleep
From something awesome
Thanks for your answer, worked for me.
Here's the code, it maybe help others
MainUI.java:
Code:
public class MainUI extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView boton =(TextView)findViewById(R.id.botonAplicar);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainUI.this, WakeUpReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainUI.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
int delay=1;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, delay);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.i("Test","Programmed");
}
});
}
}
WakeUpReceiver.java:
Code:
public class WakeUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Test","Wake Up Received");
}
}
Manifest:
Code:
<receiver android:process=":remote" android:name="WakeUpReceiver"></receiver>

How to develop a Video Collage application in android?

I am Developing an app related to "Video Collage", In this user can play multiple videos at a time in single layout file. For this i am using Video Views to play videos. but problem is user clicks save button the layout file was converted and saved as a video file into sdcard. If the user open saved file it plays multiple videos.
can any one give me an idea how can i do that?
I am using Following Code to play multiple videos at time.I am not sure I was in right direction.
Code:
public class MainActivity extends Activity {
String SrcPath = "sdcard/dhoo3.mp4";
String SrcPath2 = "sdcard/DCIM/Camera/20140206_124925.mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
//myVideoView.setVideoURI(Uri.parse(SrcPath));
myVideoView.setVideoPath(SrcPath);
// MediaController mc=new MediaController(getApplicationContext());
// myVideoView.setMediaController(new MediaController(this));
// mc.setAnchorView(myVideoView);
//mc.setMediaPlayer(myVideoView);
//myVideoView.setMediaController(mc);
myVideoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
myVideoView.start();
myVideoView.requestFocus();
mp.start();
}
});
// myVideoView.start();
final VideoView myVideoView2 = (VideoView)findViewById(R.id.myvideoview2);
//myVideoView2.setVideoURI(Uri.parse(SrcPath2));
myVideoView2.setVideoPath(SrcPath2);
// myVideoView2.setMediaController(new MediaController(this));
myVideoView2.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
myVideoView2.start();
myVideoView2.requestFocus();
mp.start();
mp.setVolume(0f, 0f);
}
});
// myVideoView2.start();
}
}

Passing data between two fragments

Hello guys,
I have a list of fragments created and I want to delete these fragment by long pressing on them. However, before doing so I want dialog fragment to pop up and offer the user the choice to delete or not. I have created the two fragments but I can't get the data across each other. I can't give functionality to the 'OK' button. Here is my code:
Code:
public class CourseListFragment extends Fragment implements
OnItemClickListener, OnItemLongClickListener {
public static final String ARG_ITEM_ID = "course_list";
public static final String YES_NO = "modify";
Activity activity;
ListView courseListView;
ArrayList<Course> courses;
CourseListAdapter courseListAdapter;
CourseDAO courseDAO;
private GetEmpTask task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
courseDAO = new CourseDAO(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.schedule_fragment_course_list,
container, false);
findViewsById(view);
task = new GetEmpTask(activity);
task.execute((Void) null);
courseListView.setOnItemClickListener(this);
courseListView.setOnItemLongClickListener(this);
return view;
}
private void findViewsById(View view) {
courseListView = (ListView) view.findViewById(R.id.list_course);
}
@Override
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Course course = (Course) list.getItemAtPosition(position);
if (course != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedCourse", course);
CustomCourseDialogFragment customEmpDialogFragment = new CustomCourseDialogFragment();
customEmpDialogFragment.setArguments(arguments);
customEmpDialogFragment.show(getFragmentManager(),
CustomCourseDialogFragment.ARG_ITEM_ID);
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// Show dialogFragment
FragmentManager fm = getActivity().getSupportFragmentManager();
CheckDialogFragment dialog = new CheckDialogFragment();
dialog.show(fm, YES_NO);
Course employee = (Course) parent.getItemAtPosition(position);
// Use AsyncTask to delete from database
courseDAO.deleteEmployee(employee);
courseListAdapter.remove(employee);
return true;
}
public class GetEmpTask extends AsyncTask<Void, Void, ArrayList<Course>> {
private final WeakReference<Activity> activityWeakRef;
public GetEmpTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected ArrayList<Course> doInBackground(Void... arg0) {
ArrayList<Course> courseList = courseDAO.getCourses();
return courseList;
}
@Override
protected void onPostExecute(ArrayList<Course> empList) {
if (activityWeakRef.get() != null
&& !activityWeakRef.get().isFinishing()) {
courses = empList;
if (empList != null) {
if (empList.size() != 0) {
courseListAdapter = new CourseListAdapter(activity,
empList);
courseListView.setAdapter(courseListAdapter);
} else {
Toast.makeText(activity, "No Course Records",
Toast.LENGTH_LONG).show();
}
}
}
}
}
/*
* This method is invoked from MainActivity onFinishDialog() method. It is
* called from CustomEmpDialogFragment when an employee record is updated.
* This is used for communicating between fragments.
*/
public void updateView() {
task = new GetEmpTask(activity);
task.execute((Void) null);
}
@Override
public void onResume() {
getActivity().setTitle("Course Schedule");
getActivity().getActionBar().setTitle("Course Schedule");
super.onResume();
}
}
//Dialog Fragment
Code:
public class CheckDialogFragment extends DialogFragment {
CourseListAdapter courseListAdapter;
CourseDAO courseDAO;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Do you want to delete?")
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
return builder.create();
}
}
I have problems trying to get the data from onItemLongClick() of CourseListFragment to CourseListFragment. Any help would be greatly appreciated.

[Q] PreferenceFragment - problems in changing language preference

I have written the following code to allow user to display in chinese or english language. But when I select the language in the settings. There is no response and change in the App. I would like to ask if anyone could see the problems. Thank you for your help!
I have created the following folders
- values-en
- values-rTW
- values-rCN
Code:
public class AppPrefActivity extends Activity {
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
SharedPreferences pref;
public static final String PREF_LANGUAGE = "language_pref";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragement()).commit();
}
public static class PrefsFragement extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(PREF_LANGUAGE))
{
Preference connectionPref = findPreference(key);
connectionPref.setSummary(sharedPreferences.getString(key, ""));
changeLanguagePref(sharedPreferences.getString(key, ""));
}
}
private void changeLanguagePref(String lang){
Locale locale = null;
if (lang.equals("Traditional Chinese")){
locale = new Locale("zh_rTW");
}else if (lang.equals("Simplified Chinese")){
locale = new Locale("zh_rCN");
}else{
locale = new Locale("en");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
this.getResources().updateConfiguration(config, null);
}
}
}
This is part of the xml for language selection
Code:
<PreferenceCategory android:title="@string/pref_cat_app">
<ListPreference
android:title="@string/pref_def_lang"
android:entries="@array/app_language"
android:entryValues="@array/app_language"
android:defaultValue="English"
android:key="language_pref"/>
you need to recreate the activity after you reload all the resources.

Categories

Resources