I created RoboSherlockMapActivity.java
import roboguice.RoboGuice;
import roboguice.activity.event.OnActivityResultEvent;
import roboguice.activity.event.OnConfigurationChangedEvent;
import roboguice.activity.event.OnContentChangedEvent;
import roboguice.activity.event.OnCreateEvent;
import roboguice.activity.event.OnDestroyEvent;
import roboguice.activity.event.OnNewIntentEvent;
import roboguice.activity.event.OnPauseEvent;
import roboguice.activity.event.OnRestartEvent;
import roboguice.activity.event.OnResumeEvent;
import roboguice.activity.event.OnStartEvent;
import roboguice.activity.event.OnStopEvent;
import roboguice.event.EventManager;
import roboguice.inject.ContentViewListener;
import roboguice.inject.RoboInjector;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import com.actionbarsherlock.view.Menu;
import com.google.android.maps.MapActivity;
import com.google.inject.Inject;
public class RoboSherlockMapActivity extends MapActivity{
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
protected EventManager eventManager;
#Inject ContentViewListener ignored; // BUG find a better place to put this
#Override
protected void onCreate(Bundle savedInstanceState) {
final RoboInjector injector = RoboGuice.getInjector(this);
eventManager = injector.getInstance(EventManager.class);
injector.injectMembersWithoutViews(this);
super.onCreate(savedInstanceState);
eventManager.fire(new OnCreateEvent(savedInstanceState));
}
#Override
protected void onRestart() {
super.onRestart();
eventManager.fire(new OnRestartEvent());
}
#Override
protected void onStart() {
super.onStart();
eventManager.fire(new OnStartEvent());
}
#Override
protected void onResume() {
super.onResume();
eventManager.fire(new OnResumeEvent());
}
#Override
protected void onPause() {
super.onPause();
eventManager.fire(new OnPauseEvent());
}
#Override
public void onNewIntent( Intent intent ) {
super.onNewIntent(intent);
eventManager.fire(new OnNewIntentEvent());
}
#Override
protected void onStop() {
try {
eventManager.fire(new OnStopEvent());
} finally {
super.onStop();
}
}
#Override
protected void onDestroy() {
try {
eventManager.fire(new OnDestroyEvent());
} finally {
try {
RoboGuice.destroyInjector(this);
} finally {
super.onDestroy();
}
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
final Configuration currentConfig = getResources().getConfiguration();
super.onConfigurationChanged(newConfig);
eventManager.fire(new OnConfigurationChangedEvent(currentConfig, newConfig));
}
#Override
public void onContentChanged() {
super.onContentChanged();
RoboGuice.getInjector(this).injectViewMembers(this);
eventManager.fire(new OnContentChangedEvent());
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
eventManager.fire(new OnActivityResultEvent(requestCode, resultCode, data));
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return false;
}
}
I inherited this and not MapActivity. Still the Action bar Sherlock doesn't show up. What might be going wrong here?
To use ActionBarSherlock with MapActivity you have to extend your Activity class from
com.actionbarsherlock.app.SherlockMapActivity which you can download from ActionBarSherlock-Plugin-Maps.
This class is a plugin for ActionBarSherlock to support MapActivity link to mentioned github project you can also find in ActionBarSherlock download section.
Related
I have a simple QR code scanning app. Here's the flowchart which describes its logic.
And this is the main part:
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class ScanActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
#Override
protected void onResume() {
super.onResume();
IntentIntegrator scanIntegrator = new IntentIntegrator(ScanActivity.this);
scanIntegrator.setOrientationLocked(false);
scanIntegrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null){
String strResult = scanResult.getContents();
Intent iii = new Intent(ScanActivity.this, ScanResultActivity.class);
iii.putExtra("scan_result", strResult);
startActivity(iii);
}
}
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
}
If you are in ScanActivity, then the app will immediately display ScanResultActivity after scanning, which is as expected. The only issue is app won't quit when Back key is pressed. Instead, it will jump to ScanResultActivity. Of course, the scanning result is null.
How to quit the app by pressing Back key when scanning is running?
Full code: https://github.com/anta40/QRScanDemo
this method
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
should call the super method, like this
#Override
public void onBackPressed() {
super.onBackPressed();
}
I want perform some check continuously. I have defined AsyncTask as
follows,
new AsyncTask<String, Void, String>() {
#Override
protected void onPreExecute() {
Log.d(TAG, "onPreExecute()");
}
#Override
protected String doInBackground(String... params) {
sendData(array);
return null;
}
#Override
protected void onPostExecute(String res) { }
}.execute(userResponse);
But when I terminate the application then thread stops execution.
Service class demo :-
package com.example.My Application;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
Activity :-
package com.example.My Application;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// Method to start the service
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
Manifest :-
Add <service android:name=".MyService" />
When I put setResult() in the onClickListener of a button, that works. However, if I put it in onPause(), onStop() or onDestroy(), that never works.
I got really confused about that.
Here is the code of MainActivity.
package com.example.hellotest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
startActivityForResult(new Intent(getApplicationContext(), SecondActivity.class), 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "hello world", Toast.LENGTH_SHORT).show();
Log.d(TAG, "RESULT_OK");
} else {
Toast.makeText(getApplicationContext(), "result not ok", Toast.LENGTH_SHORT).show();
Log.d(TAG, "RESULT_CANCELED");
}
}
}
Here is the SecondActivity.
package com.example.hellotest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity {
private static final String TAG = SecondActivity.class.getSimpleName();
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// setResult(RESULT_OK);
Log.d(TAG, "button on click");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
#Override
protected void onDestroy() {
setResult(RESULT_OK);
Log.d(TAG, "onDestroy");
super.onDestroy();
}
#Override
protected void onPause() {
// setResult(RESULT_OK);
Log.d(TAG, "onPause");
super.onPause();
}
#Override
protected void onStop() {
// setResult(RESULT_OK);
Log.d(TAG, "onStop");
super.onStop();
}
}
And here is the Log of which I put setResult() in the onClick()
11-21 00:07:54.243: D/SecondActivity(4790): button on click
11-21 00:07:56.364: D/SecondActivity(4790): onPause
11-21 00:07:57.813: D/SecondActivity(4790): onStop
11-21 00:07:57.813: D/SecondActivity(4790): onDestroy
11-21 00:07:56.404: D/MainActivity(4790): RESULT_OK
But if in onDestroy()
11-21 00:10:08.125: D/SecondActivity(4846): onPause
11-21 00:10:09.456: D/SecondActivity(4846): onStop
11-21 00:10:09.456: D/SecondActivity(4846): onDestroy
11-21 00:10:08.176: D/MainActivity(4846): RESULT_CANCELED
I have no idea about that.
You can also override finish() and set the result before calling into the super class:
#Override
public void finish() {
setResult(...);
super.finish();
}
Call setResult() when the user performs the operation that selects the result. onPause() or later lifecycle events are too late.
startActivityForResult() is designed for "picker" sorts of activities, where the user is picking something. The typical pattern is for you to call setResult() and finish() once the user has picked their item.
My code for implementing speech recognition using SpeechRecognizer class seem correct....yet it does not function whatsoever. None of the log statements in any of the RecognitionListener methods are being logged.
I have follow the Android reference docs on this quite carefully and I am not seeing what I am missing.
I have added the record audio permission.
package com.example.voicetest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class VoiceActivity extends Activity {
SpeechRecognizer recognizer;
TextView display;
private boolean listening;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
listening=false;
display=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
recognizer=SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(
new RecognitionListener()
{
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onBeginning");
}
#Override
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
Log.i("test","onBuffer");
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.i("test","onEndOfSpeech");
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
Log.i("test","onError");
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
Log.i("test","onPartial");
}
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
display.setText("Ready and waiting..");
Log.i("test","onReady");
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
Log.i("test","onResults");
String result=stringListToString(results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION));
display.setText(result);
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.voice, menu);
return true;
}
public void buttonOnClick(View v)
{
if(!listening)
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
recognizer.startListening(intent);
Log.i("test","recognizer listening");
listening=true;
button1.setText("Stop Listening");
}
else
{
recognizer.stopListening();
listening=false;
Log.i("test","recognizer stopped");
button1.setText("Listen");
}
}
private String stringListToString(ArrayList<String> strings)
{
String result="";
for(int i=0;i<strings.size();i++)
{
result=result+strings.get(i)+'\n';
}
return result;
}
#Override
public void onPause()
{
recognizer.stopListening();
}
}
You also need
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
Try the following:
Log out the error code. If it is giving server error then you are not connected to the internet.
If you want to use offline speech recognition, you need language models downloaded. Just verify that Speech input works on your keyboard (Press the mic button, say something and see if it is getting converted). If it is not getting recognized, then there is no error with your code, you just need to download speech models.
I'm having trouble with converting my code to fragment
this is my code when my app is still an activity
package com.ronnielp.loginsample2;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.model.people.Person;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class SignIn extends Activity implements OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener{
private static final int REQUEST_CODE_RESOLVE_ERR = 40;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private TextView txtUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlusClient =new PlusClient.Builder(this, this, this)
.setScopes(Scopes.PLUS_LOGIN)
.setVisibleActivities("http://schemas.google.com/AddActivity")
.build();
findViewById(R.id.sign_in_button).setOnClickListener(this);
txtUser = (TextView) findViewById(R.id.txtUser);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mPlusClient.connect();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mPlusClient.disconnect();
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
mConnectionResult = result;
}
#Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Person user = mPlusClient.getCurrentPerson();
String acc = mPlusClient.getAccountName();
txtUser.setText(acc);
}
#Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected() && mConnectionResult !=null){
try{
mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
} catch (SendIntentException e){
mConnectionResult = null;
mPlusClient.connect();
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE_RESOLVE_ERR && resultCode == RESULT_OK){
mConnectionResult = null;
mPlusClient.connect();
}
}
}
and when i convert it to fragment my code doesnt work anymore
please help me I'm still a newbie in Android programming
Number of thing may have gone wrong. One issue would be accessing your the childFragmentManager. The manager will not pass the result to the fragment, you have to do that manually in your base Class.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
if(fragment != null){
fragment.onActivityResult(requestCode, resultCode, intent);
}
}