Getting GPS Location Crashes

by Bobbie » Wed, 05 Nov 2008 23:32:40 GMT


Sponsored Links
 I've posted with no help, please help me out.  I am trying to figure
out how to get the GPS location of my device.  However, when I use the
code below (see "CODE TO GET LOCATION") my Android application
crashes.  I am developing on the G1 phone.  If I change the provider
to "network" instead of "gps", it works perfectly fine.  Here is the
code I am using:

** ANDROID MANIFEST PERMISSIONS **
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android:permission.ACCESS_FINE_LOCATION" />

** CODE TO GET LOCATION **
LocationManager lM = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationProvider lP = lM.getProvider("gps");
Location loc = lM.getLastKnownLocation(lP.getName());

Do I have to somehow tell the GPS to try to get a fix?  If so, how do
I go about that?
--~--~---------~--~----~------------~-------~--~----~



Getting GPS Location Crashes

by Mark Murphy » Wed, 05 Nov 2008 23:41:05 GMT


 


And the error message is...what?

It is difficult to tell you what a crash means without the Java traceback.

You can get this from DDMS or adb logcat.


At this point, loc is probably null.


Yes. The device does not keep the GPS radios on all the time, otherwise 
battery life would be worse than it already is. :-(

Try using requestLocationUpdates(). It still may take a while for you to 
get a fix -- I ran a test yesterday and it took ~15 seconds from the 
time my app started until a fix was ready.

This, of course, assumes your device is configured to allow GPS access 
at all:

Settings > Security & Location > Enable GPS satellites

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.4 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. Key Dispatching Time Out problem

Hi, I am trying to buffer a video stream and play the video from the
buffer file, but it seems not to work. I check the logcat, it says
something about Key "Dispatching Time Out problem". Does anyone know
what's wrong? The problem is probably due to CPU consuption because
this error appears after I wrong the same programme several times.
Here is my code.

package com.example.Client2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.webkit.URLUtil;
import android.widget.ImageButton;
import android.widget.Button;
import android.util.Log;
import android.view.View;

//import android.view.Display;
//import android.view.WindowManager;
//import android.view.ViewGroup.LayoutParams;
//import android.widget.RelativeLayout;
//import android.view.Gravity;



public class Client2 extends Activity implements
                                      OnBufferingUpdateListener,
                                      OnCompletionListener,
                                      MediaPlayer.OnPreparedListener,
                                      SurfaceHolder.Callback{

        private static final String TAG="MediaPlayer";
        private MediaPlayer         mMediaPlayer;
        private SurfaceView         mPreview;
        private SurfaceHolder       holder;
        private String              path;
        private int                 mVideoWidth;
        private int                 mVideoHeight;
    private static final int LOCAL_VIDEO = 1;
    private static final int STREAM_VIDEO = 2;
        //private static final int CENTER = 17;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mPreview=(SurfaceView) findViewById(R.id.surface);
        holder=mPreview.getHolder();
        holder.addCallback(this);
        //holder.setFixedSize(30, 30);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        ImageButton Play = (ImageButton) findViewById(R.id.play);
        ImageButton Pause = (ImageButton) findViewById(R.id.pause);
        Button Stream = (Button) findViewById(R.id.stream);

        //checkOrientation();

        Play.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                        playVideo(LOCAL_VIDEO);
                }
        });

        Pause.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                        mMediaPlayer.pause();
                }
        });

        Stream.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                        playVideo(STREAM_VIDEO);
                }
        });
    }

/*  private void checkOrientation()
    {
         WindowManager wm = getWindowManager();
         Display d = wm.getDefaultDisplay();
                 Log.d(TAG,"ok1");

         LayoutParams params;

         Log.d(TAG,"ok2");
         if (d.getWidth() > d.getHeight())
         {
                Log.d(TAG,"ok3");
             //---landscape mode---
             params = new RelativeLayout.LayoutParams(200,200);
             Log.d(TAG,"ok4");

         }
         else
         {
             //---portrait mode---
             params = new RelativeLayout.LayoutParams(200,200);
         }
             Log.d(TAG,"ok5");
         mPreview.setLayoutParams(params);
    }*/

    private void playVideo(Integer Media)
    {
        try
        {
                switch (Media){
                case LOCAL_VIDEO:
                path="/sdcard/toystory3.3gp";
                break;
                case STREAM_VIDEO:
            Log.d(TAG,"check");
                //path="rtsp://172.17.179.200:5544/stream";
            path="http://172.17.179.200:8080/stream";
                break;
                }

                mMediaPlayer=new MediaPlayer();
                Log.d(TAG,"Here...");
            //setDataSource(path);
                mMediaPlayer.setDataSource(path);
                mMediaPlayer.setDisplay(holder);
                mMediaPlayer.prepare();

                mMediaPlayer.setOnBufferingUpdateListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setOnPreparedListener(this);

                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                Log.d(TAG,"Debug");
                mVideoWidth=mMediaPlayer.getVideoWidth();
                mVideoHeight=mMediaPlayer.getVideoHeight();
                holder.setFixedSize(mVideoWidth,mVideoHeight);
                        mMediaPlayer.start();
                        //mMediaPlayer.stop();
                        //mMediaPlayer.release();
        }
        catch (Exception e)
        {
                Log.e(TAG,"error:"+e.getMessage(),e);
        }
    }

    private void setDataSource(String path) throws IOException{
        //Log.d(TAG,"Here...");
        if (!URLUtil.isNetworkUrl(path)){
                //mMediaPlayer.setDataSource(path);
        }else{
                Log.d(TAG,"Here...");
                URL url=new URL(path);
                URLConnection cn = url.openConnection();
                Log.d(TAG,"Hereok...");
                cn.connect();
                Log.d(TAG,"Hereok1...");
                InputStream stream = cn.getInputStream();
                if (stream==null)
                        throw new RuntimeException("stream is null");
                //Log.d(TAG,"Hereok...");
                File temp=File.createTempFile("mediaplayertmp","dat");
                String tempPath=temp.getAbsolutePath();

                FileOutputStream out=new FileOutputStream(temp);

                byte buf[]=new byte[20];
                do{
                        int numread=stream.read(buf);
                        if(numread<=0)
                                break;
                        out.write(buf, 0, numread);
                        Log.d(TAG,"Hereok...");
                }while(true);
                mMediaPlayer.setDataSource(tempPath);
                try{
                        stream.close();
                }catch(IOException ex){
                        Log.e(TAG,"error:"+ex.getMessage(),ex);
                }
        }
    }
        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
                // TODO Auto-generated method stub

        }
        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
                Log.d(TAG,"SurfaceCreated called");
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
                // TODO Auto-generated method stub

        }
        @Override
        public void onPrepared(MediaPlayer arg0) {
                Log.d(TAG,"OnPrepared called");
                //playVideo();
        }
        @Override
        public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub

        }
        @Override
        public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
                // TODO Auto-generated method stub

        }
}

-- 

2. project help

i guess im very new. ive made my first hello world app and played with the
api's. its fun but very frustrating.
i have great ideas and months of research.( im a marketing grad from  WVU
class of 2005 and doing well in the automotive field  )
as of late i have been trying to diversify myself outside of the auto
sector. ive been in  domain sales for years and have a large portfolio.
but im seeking more from my skill set .what im looking for is someone that
would like to guide me. help me grow as a android developer and share in my
splendor of ideas.
i have a google merch account and all things needed to deploy to the android
market. and willing to split all earnings in half.
thank you for your time and if interested please respond privately to
mike.d.k...@gmail.com im looking forward to hearing from you.

thanks again
mike

-- 

3. bitmap size exceeds VM budget

4. How can I use different row view for selected or unselected list item?

5. Video streaming from android mobile phone to a remote computer

6. How to get Android Source code?

7. Killi Process with files on SD card