I wrote some code which basically spits out NMEA sentences. The code worked on froyo and did exactly what I wanted. Now However, when I got the gingerbread upgrade, it is no longer working. Has anyone else had this issue?
I'm using a Nexus One and will post some code later if you guys need it.
Thanks
Sorry, i was very busy lately. Here is the code finally:
public class GPSTest extends Activity {
TextView mTextView;
Button mStartButton, mStopButton;
LocationManager mLocationManager;
boolean isRegistered;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.mTextView);
mStartButton = (Button) findViewById(R.id.Button01);
mStopButton = (Button) findViewById(R.id.Button02);
mStartButton.setOnClickListener(mButtonListener);
mStopButton.setOnClickListener(mButtonListener);
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
}
void registerListeners(){
if(!isRegistered){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0 , 0, mLocationListener);
mLocationManager.addNmeaListener(mListener);
isRegistered=true;
}
}
void deregisterListeners(){
if (isRegistered){
mLocationManager.removeUpdates(mLocationListener);
mLocationManager.removeNmeaListener(mListener);
isRegistered=false;
}
}
OnClickListener mButtonListener = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v==mStartButton){
registerListeners();
}else{
deregisterListeners();
}
}
};
#Override
protected void onPause(){
super.onPause();
deregisterListeners();
}
#Override
protected void onResume(){
super.onResume();
registerListeners();
}
NmeaListener mListener = new NmeaListener(){
#Override
public void onNmeaReceived(long timestamp, String nmea) {
// TODO Auto-generated method stub
mTextView.append("\n"+nmea);
}
};
LocationListener mLocationListener = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
;
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}
Documented bug in the post-Froyo implementations on some handsets (Nexus One I can confirm, Xoom anecdotally seems to be fine). Haven't heard from the folks using the Nexus S, but it sounds like they are fine.
See http://code.google.com/p/android/issues/detail?id=15500 for more detail.
Edit: trying to poke the folks at Google to see if some clarification can be found.
Edit 2: Oh yeah, verified that 2.2 works (HTC Incredible, Nexus One worked BEFORE the 2.3 update), but the same Nexus One no longer works after updating to 2.3.3.
Related
public class LocationService extends Service {
private Handler mHandler = new Handler();
private Timer mTimer = null;
private int mCount = 0;
public static final long NOTIFY_INTERVAL = 10 * 1000;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// cancel if already existed
if (mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
#Override
public void onDestroy() {
mTimer.cancel();
}
private class TimeDisplayTimerTask extends TimerTask implements LocationListener {
#Override
public void run() {
mHandler.post(new Runnable() {
#Override
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000, 0,
TimeDisplayTimerTask.this);
}
});
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
Toast.makeText(getApplicationContext(), Double.toString(location.getLat(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), Double.toString(location.getLng(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Cant Get Location", Toast.LENGTH_LONG).show();
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
This code work fine on emulator. I can get location every 10 second. But when I test it on real phone, I don't see anything appear at all. Is it anything wrong with this code? Please show me how to fix this.
Can you verify that your service is not being killed by the device on background?
(You can see this on Settings > Apps > Running tab > [Your app name] There should be a process count and service count there)
Android devices has it's own way if killing services on background. If that's the case, you should start your service on foreground. To prevent it from being killed.
How you ask? You should add this on start of your service:
Notification notification = new Notification();
startForeground(1, notification);
What is the notification you ask? This prompts the user when you're app is on background, you can substitute your toast with this. Here's a link related to its this. Check the accepted answer.
Android - implementing startForeground for a service?
I was wondering, how can I keep the device listening for voice commands with voice recognition while the device is asleep? The idea I have is that I would like the device to respond to my voice, even if I have the screen locked or the screen has timed out.
Is this possible? I have tried using this as a service and an interface and it stops listening once the screen locks. Can I receive any help with this? This is my class.
public class VoiceEngineService extends Activity {
private boolean isSpeakingDone = false; // default setting
private SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(this);
private AudioManager mAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_for_speech);
// mute beep sound
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
sr.setRecognitionListener(new listener());
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); // LANGUAGE_MODEL_WEB_SEARCH
i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication()
.getClass().getName());
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 6);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
i.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5500);
sr.startListening(i);
}
class listener implements RecognitionListener {
#Override
public void onReadyForSpeech(Bundle params) {
// TODO Auto-generated method stub
}
#Override
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onRmsChanged(float rmsdB) {
// TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
// TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
#Override
public void onError(int error) {
// TODO Auto-generated method stub
if (SharedPref.getVoiceController() == false) {
sr.cancel();
Intent i = new Intent();
sr.startListening(i);
} else {
sr.stopListening();
sr.destroy();
finish();
}
}
#Override
public void onResults(Bundle results) {
// TODO Auto-generated method stub
isSpeakingDone = true;
ArrayList<String> mDataList = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Intent i = new Intent();
i.putStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS, mDataList);
setResult(RESULT_OK, i);
finish();
}
#Override
public void onPartialResults(Bundle partialResults) {
// TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
// TODO Auto-generated method stub
}
} // end listener class
#Override
protected void onPause() {
if ((isSpeakingDone == false)) {
finish();
}
super.onPause();
}
#Override
protected void onStop() {
// when speaking is true finish() has already been called
if (isSpeakingDone == false) {
finish();
}
super.onStop();
}
#Override
protected void onDestroy() {
sr.stopListening();
sr.destroy();
super.onDestroy();
}
}
You have to implement your voice listener in a service. Create a class that extends 'Service' and make up some logic to take care of recording.
If you tried already the service, then it might be that you tried to redirect commands to an activity which most likely has been stopped by Android OS. Generally when talking about doing stuff when phone is in lock mode, you only can hope to accomplish tasks in one or more services coupled togethe.
When you are in Activity, of course wen the activity goes out of scope it will be shut down by Android OS. but services can still run in background unless shut dow mm explicitly by your own code or in rare cases that Android will recognize that it needs memory and processor power for other tasks.
The application doesnt point to where I am at..here is what i have done:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mv=(MapView) findViewById (R.id.mapView);
mv.setBuiltInZoomControls(true);
// mv.setSatellite(true);
mv.setStreetView(true);
lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
llistener=new MyLocationListener();
lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, llistener);
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location!=null)
{
Toast.makeText(getBaseContext(),"Location changed: "+location.getLatitude()+" lang: "+ location.getLongitude() , Toast.LENGTH_SHORT).show(); // no toast is shown
}
p=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
mapC.animateTo(p);
mapC.setZoom(18);//no zooming happens
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
To my manifest I added both of these:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
first of all sorry for poor english. when the location was changed and you set the value into the map you need to invalidate the map using the invalidate() method of MapView class so you can get the updated value on the map
mapC.animateTo(p);
mapC.setZoom(18);
mv.invalidate();
as well as add after you set the value at initial time set into the map like
mv.setBuiltInZoomControls(true);
mv.setStreetView(true);
mv.invalidate();
Although I set minTime to 10 seconds in the requestLocatioupdate method I still get new location in less than one second.
public class GpsActivity extends Activity {
LocationManager mLocationManager;
TextView mTextView;
int count;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text);
count = 0;
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,mGpsLocationListener);
}
Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what){
case 1:
mTextView.setText("new location count:"+count);
}
}
};
LocationListener mGpsLocationListener = new LocationListener(){
#Override
public void onLocationChanged(Location location) {
count++;
mHandler.sendEmptyMessage(1);
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
}
My program is written for android 2.2
Any idea?
Thanks in advance.
you can set a distance .if not solve,youcan see :minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance the minimum distance interval for notifications, in meters
so i think the time not correct,if you hope update 10s,i think you should use Time andTimeTask,or handler. or you can look http://androidforums.com/developer-101/107085-proper-provider-parameter-requestlocationupdates.html
I am trying to get the current GPS location of the mobile. I am using emulator I have given the position to the emulator using DDMS but I am getting nothing in the screen also I don't get any errors.
public class getitright extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener ll=new LocationListener(){
public void onLocationChanged(Location l)
{
go(l);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
public void go(Location l){
TextView tv=(TextView)findViewById(R.id.tv1);
tv.setText(""+l.getLatitude());
}
}
Put some log messages in for a start.
But secondly. You shouldn't declare your TextView or your LocationListener to be local variables like that. Those should both be member variables. By declaring your locationlistener like that, you lose your reference to it. That could be the problem, but if it's not, you're still unable to remove the locationlistener from the manager because you don't have a reference.
Are you sure your textview is even showing at all? Try setting it to something static in onCreate just to make sure
Also add an #Override statement to
public void onLocationChanged(Location l)
{
go(l);
}
Just to be 100% sure you're actually overriding the method.