Why getVoiceInteractor () returns null? - java

I'm trying to get VoiceInteractor, but the getVoiceInteractor() method always returns null. In the manifest, I wrote the following:
<activity android:name=".Activity_2">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE" />
</intent-filter>
</activity>
Activity_1:
protected void onCreate(Bundle savedInstanceState) {
startActivity(new Intent(Activity_1.this, Activity_2.class));
}
Activity_2:
protected void onCreate(Bundle savedInstanceState) {
VoiceInteractor mInteractor;
mInteractor = getVoiceInteractor();
}

The documentation seems a bit unclear, but I found the following clues:
startLocalVoiceInteraction() seems to be needed to be able to get the VoiceInteractor
onLocalVoiceInteractionStarted() seems to indicate when you can call getVoiceInteractor
The last clue came from onGetDirectAction where it states:
To get the voice interactor you need to call getVoiceInteractor() which would return non null only if there is an ongoing voice interaction session
This means your code should look something like this:
public class YourActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
startLocalVoiceInteraction(new Bundle());
}
#Override
public void onLocalVoiceInteractionStarted() {
getVoiceInteractor(); // Should be non-null here
}
}

Related

Issues re-starting a finish()ed android activity

I have two android activities with one single button on both.
The activity DetailsActivity is my MAIN activity.
public class DetailsActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.details_activity_layout);
}
#Override
public void onResume() {
super.onResume();
}
public void gotoSubDetails(View view) {
Intent intent = new Intent(this, SubDetailsActivity.class);
startActivity(intent);
finish();
}
}
And my SubDetailsActivity is as follows:
public class SubDetailsActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.sub_details_activity_layout);
}
#Override
public void onResume() {
super.onResume();
}
public void gotoDetails(View view) {
Intent intent = new Intent(this, DetailsActivity.class);
startActivity(intent); // Restarting the finish()ed activity here.
finish();
}
}
And this is how I mentioned my button in details_activity_layout.xml for DetailsActivity.java:
<Button android:id="#+id/details_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/to_sub_details"
android:onClick="gotoSubDetails" />
And this is how I have mentioned my button in sub_details_activity_layout.xml for SubDetailsActivity.java:
<Button android:id="#+id/sub_details_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/back_to_details"
android:onClick="gotoDetails" />
These are my both activities in AndroidManifest.xml:
<activity android:name="DetailsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SubDetailsActivity">
</activity>
When I click the button in DetailsActivity, the activity finish()es properly and SubDetailsActivity starts up.
But when I click the button in SubDetailsActivity to get back to the finish()ed DetailsActivity, the app straight away crashes on my LG L90 phone.
Where am I going wrong? Any help? Please.
PS:
I cannot post the log cat report as I did not test it on my mac but directly on my phone. I don't have an emulator. I am compiling the code on my terminal and transferring the .apk file on my phone via bluetooth.
EDIT:
Ok guys. Now this is really funny!
I removed the
android:onClick="gotoSubDetails"
and
android:onClick="gotoDetails"
from my details_activity_layout.xml and sub_details_activity_layout.xml respectively. And I loaded the android.widget.Button's in my DetailsActivity.java and SubDetailsActivity.java and added an android.view.View.OnClickListener on both of them and overrode the onClick(View) method.
This is what I did in my DetailsActivity.java:
public class DetailsActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.details_activity_layout);
Button button = (Button)findViewById(R.id.details_submit);
button.setOnClickListener(new DetailsSubmitListener());
}
#Override
public void onResume() {
super.onResume();
}
public class DetailsSubmitListener implements OnClickListener {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, SubDetailsActivity.class);
startActivity(intent);
finish();
}
}
}
And this is what I did in my SubDetailsActivity.java class:
public class SubDetailsActivity extends Activity {
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.details_activity_layout);
Button button = (Button)findViewById(R.id.sub_details_submit);
button.setOnClickListener(new SubDetailsSubmitListener());
}
#Override
public void onResume() {
super.onResume();
}
public class SubDetailsSubmitListener implements OnClickListener {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, DetailsActivity.class);
startActivity(intent);
finish();
}
}
}
And this miraculously worked fine.
So what was the problem with the .xml attributes in my layout files? And how different is it from the hardcoded listener in my .java file? This thing has confused me. finish()ed activities can be re-started. Thats what I discovered with my change in the program. Please shed some light of knowledge on this.
Aditya,
If you want to traverse between 2 activities like in your case, you should never finish Details activity. Instead without finishing Details activity, start SubDetails activity. If you want to come back, no coding required. If user presses BACK button, SubDetails Activity will be finished and DetailsActivity will come to foreground.
Finishing one activity and starting it again, is unnecessary overhead. Avoid it. Finish activity if it is absolutely necessary.
Also, there is no any way to restart finished activity.
Just like Harry answered,
I want to add something for you. If you are programming for higher API Levels (e.g. Android API 13+) you can define a parent activity.
This way you don't need to make such complex implementations.
For reference
Google Developers Guide to UP navigation
Stackoverflow ANSWER on the topic
I think you should read this!:
http://developer.android.com/guide/components/tasks-and-back-stack.html
I think it's very important how you declared the launchMode in your andriodManifest for your activities ! There should be the problem. Read carefully what is a task and how a back stack in android works.
Maybe you are finishing the task but at same time you are viewing the next activity which is in the same task ! After reopen it crashed because of this I think. Hope it helps.

How to prevent theme.dialog activity to allow outside touch?

I have an activity that is using the Theme.Dialog style. It's a popup for my quiz game, for the wrong answer. But I have a problem. User can click outside the popup dialog themed activity and click on the next question. How to prevent that? I blocked back button and that works fine.
Also, when a user clicks on the popup or outside of it, it starts counting the ON time again. My popup stays ON for 2500 ms. How to prevent that also?
So, basically I don't want to allow any click outside my popup and don't want to reset my delay time when someone clicks on the screen.
Here's the code of the popup window:
public class WrongAnswer extends Activity{
MediaPlayer sound;
TextView wrong;
String correctAnswer, correct;
public final int delayTime = 2500;
private Handler myHandler = new Handler();
public void onUserInteraction(){
myHandler.removeCallbacks(closePopup);
myHandler.postDelayed(closePopup, delayTime);
}
private Runnable zatvoriPopup = new Runnable(){
public void run(){
finish();
}
};
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wrong);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
tacno = extras.getString("correctAnswer");
}
inicijalizujVarijable();
myHandler.postDelayed(closePopup, delayTime);
}
private void inicijalizujVarijable() {
wrong = (TextView) findViewById(R.id.tvWrong);
wrong.setText("Wrong answer!\nCorrect answer is:\n\n" + correct);
}
}
My activity in manifest:
<activity
android:name="com.myquiz.myquizgame.WrongAnswer"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="com.myquiz.myquizgame.WRONGANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If I got you correctly, this question had been already asked. Here is the answer, and according to it you need to call a setter method on activity, that will close your activity dialog:
this.setFinishOnTouchOutside(false);
I hope it will help you.
Second, every time when user touches WrongAnswer activity — you start new delayed task and cancel previous one here:
public void onUserInteraction() {
myHandler.removeCallbacks(zatvoriPopup);
myHandler.postDelayed(zatvoriPopup, delayTime);
}
that's why you have problems with timer

How to handle screen orientation change

I'm trying to handle screen orientation change for my android application but without success.
Here's my manifest :
<activity
android:name="fr.ups.l3info.l3info_catchgameactivity.CatchGameActivity"
android:label="#string/app_name"
android:screenOrientation="user"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and I adde this function in my activity class :
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
But when I change the screen orientation the application is recreated and this function is never executed. What I have done wrong? Thanks for your help.
Use this in your manifest
android:configChanges="keyboardHidden|orientation|screenSize|layoutDirection"
You might want to read more about activity lifecycle. More you can find here (http://developer.android.com/training/basics/activity-lifecycle/index.html)
But more important is to get familiar with your activity state.
Method you are looking for is not onConfigurationChanged(Configuration newConfig), but onSaveInstanceState(Bundle savedInstanceState).
Now in your OnCreate() method you will need to check if there is some state already saved, and then recreate that situation.
There is a very good explanation on this link: Saving Android Activity state using Save Instance State
But basically what you need to do is this:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and then with that in mind:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
} else {
// Probably initialize members with default values for a new instance
}
...
}
STATE_SCORE and STATE_LEVEL are some public static final String values that are used to somehow label your stuff you want to save.
For example, if you have EditText view, and you type something in, then rotate your screen, that value will be lost. But in your onSaveInstanceState() method you shall use that Bundle parameter and put value of your EditText view as a String.
With that saved, now you can get that value in your onCreate() method and set the value of your EditText view to it.
Hope this helps :)
If you're trying to define a "different layout" to your app when the orientation changes, just define a new layout give it the same name as your other layout and put it under res/layout-land.
So that when your application recreates itself and calls setContentView(layout_name) within the onCreate function it will call the under res/layout-land and not the one under res/layout .

Android OpenGL ES 2.0 Live Wallpaper - GLSurface methods not calling

I follow next tutorial (first part of it), but with slightly simple class structure
http://www.learnopengles.com/how-to-use-opengl-es-2-in-an-android-live-wallpaper/
I get next chain of events when i try to select wallpaper (preview)
onCreateEngine()->onCreate() -> onResume()->onPause()->onResume()->onPause()
Why i get onResume()->onPause() two times?
When i hit "Set wallpaper" i get extra onPause()->onResume() and live wallpaper crashes (well maybe because onPause() called 2 times)
Also i don't see calls of OGLES2Renderer like onSurfaceCreated(), onDrawFrame() in preview or on setting wallpaper.
What i missed out of view?
Android manifest (part of it)
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service
android:name="com.aristarhys.lw.LW"
android:label="#string/app_name"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/lw" />
res/xml/lw.xml
<?xml version="1.0" encoding="UTF-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="#drawable/ic_launcher"
android:description="#string/app_desc"/>
LW.java
public class LW extends WallpaperService
{
public class OGLES2Engine extends Engine
{
private class OGLES2SV extends GLSurfaceView
{
public OGLES2SV(Context context)
{
super(context);
}
public void onDestroy()
{
super.onDetachedFromWindow();
}
}
private OGLES2SV SV = null;
#Override
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
SV = new OGLES2SV(LW.this);
SV.setEGLContextClientVersion(2);
SV.setRenderer(new OGLES2Renderer());
}
#Override
public void onVisibilityChanged(boolean visible)
{
if (!visible)
SV.onResume();
else
SV.onPause();
super.onVisibilityChanged(visible);
}
#Override
public void onDestroy()
{
SV.onDestroy();
super.onDestroy();
}
}
#Override
public Engine onCreateEngine()
{
return new OGLES2Engine();
}
}
OGLES2Renderer.java
public class OGLES2Renderer implements GLSurfaceView.Renderer
{
private static final String LOG_TAG = "GL2Surface";
#Override
public void onDrawFrame(GL10 unused)
{
Log.i(LOG_TAG, "Draw frame");
}
#Override
public void onSurfaceChanged(GL10 unused, int width, int height)
{
Log.i(LOG_TAG, "Surface changed");
}
#Override
public void onSurfaceCreated(GL10 unused_, EGLConfig _unused)
{
Log.i(LOG_TAG, "Surface created");
}
}
You can use andengine Wallpaper extension to implement this.

Activity switching not working android

I am working on an android app that has two activities. One is the menu screen and the other is the one that the user actually uses once they have made a selection. The second activity does all of its processing in a worker thread. When I make the selection to move on to the second activity, it "flashes" the activity for about 100ms. I found some logs that look like they would tell me what I need to fix if I knew what they meant.
02-29 10:52:33.850: I/ActivityManager(1255): Starting activity: Intent { cmp=com.wcf.imageShare/.ImageShareActivity }
02-29 10:52:33.920: W/InputManagerService(1255): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy#45c0a198 (uid=10097 pid=24918)
02-29 10:52:33.936: W/WindowManager(1255): No window to dispatch pointer action
02-29 10:52:33.952: I/ActivityManager(1255): Displayed activity com.wcf.imageShare/.ImageShareActivity: 96 ms (total 96 ms)
02-29 10:52:34.000: W/WindowManager(1255): No window to dispatch pointer action 1
The program itself is not crashing, it just takes me back to the menuview and displays my choices again. Here is the code I am using to switch activities
lstServers.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Beacon.activeServer = position;
Intent uploadIntent = new Intent(inst,ImageShareActivity.class);
inst.startActivity(uploadIntent);
}
});
inst holds the 'this' variable of the activity that the code is running in since I couldn't reference it by using 'this'
Here is the onCreate of the activity I am trying to switch to:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
progBar = (ProgressBar)findViewById(R.id.progressbar_Horizontal);
tv = (TextView)findViewById(R.id.prog_txt);
btnCancel = (Button)findViewById(R.id.cancel);
btnCancel.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
try {
uploaderThread.interrupt();
Uploader.CancelUpload();
} catch (Exception e) {
e.printStackTrace();
}
}
});
uploaderThread.start();
}
catch(Exception e)
{
Log.d("Uploader ERROR",e.getMessage());
}
}
And finally the part of the manifest file that has to do with the two activities.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" android:debuggable="true">
<activity
android:name=".ImageShareActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
<activity android:name=".ServerChoiceActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<data android:mimeType="image/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
Does anyone have any suggestions or know of something that I am missing?
Thanks
Nick Long
Change this to this :
lstServers.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Beacon.activeServer = position;
Intent uploadIntent = new Intent(your_current_activty.this,ImageShareActivity.class);
uploadIntent.startActivity();
}
});

Categories

Resources