服务系列问题

时间:2014-05-11 18:53:48

标签: android

无论何时需要启动应用程序崩溃,服务都无法启动......

这就是我的服务代码:

public class GPSTRACKER extends Service  {

    //private final Context mContext=getBaseContext();

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; 

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES =  60 ; 

    // Declaring a Location Manager
    protected LocationManager locationManager;

    //public GPSTRACKER(Context context) {
        //this.mContext = context;

    //}
    WakeLock wakeLock;

public Location getLocation() {

        locationManager = (LocationManager) 
                getSystemService(LOCATION_SERVICE);

         //getting GPS status
         isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
            Log.d("Network", "Network");
            if (locationManager != null) {
                location = locationManager
            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        }
        }

            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        }
                    }
                }



        return location;
    }



    @Override

    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
    }
    @Override
    public void onStart(Intent intent, int startId) {

        Bundle extras = intent.getExtras();

                if (extras != null && !extras.isEmpty()) {  // has effect of unparcelling Bundle


                    String message = intent.getStringExtra("message");
                    int p=0,j,i=1,t,success=0;
                    double[] d = new double[1000];

                    Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(message);

                    while(m.find())
                    {
                       double  k = Double.parseDouble(m.group(1));
                       d[p]=k;
                       p++;
                       }
                     double line;
                     double[] ship = new double[1000] ;
                     double[] b = new double[1000] ;


                    ship[0]=SlopeCalc(d[2],d[0],d[3],d[1]);
                    b[0]=d[0]-ship[0]*d[1];
                    System.out.println(ship[0]);

                    if (p>3)
                    {


                    for(j=2;j<p;j++)
                    {
                        if(j+2<p){
                        ship[i]=SlopeCalc(d[j+2],d[j-2+2],d[j+1+2],d[j-1+2]);
                        b[i]=d[j]-(ship[i]*d[j+1]);

                        j++;
                        i++;
                        }
                        else{
                            break;
                        }


                    }

                    }

                    while(true)
                    {



                     latitude = location.getLatitude();
                     longitude = location.getLongitude();

                    for (t=0;t<i;t++){
                    line=ship[t]*longitude+b[t]-latitude;
                    System.out.println(line);
                    if (line>-0.001 && line<0.001){
                        success=1;
                        break;
                    }
                    }
                    if (success==1){
                    break;
                    }

                    }


                        System.out.println(success);

                }
                else
                {
                    Log.i("Log", "Bundle is null");
                }

    }
    public static double SlopeCalc(double y2,double y1, double x2,double x1){

        double sou;
        sou=(y2-y1)/(x2-x1);
        return sou;
    }


    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     * */
            public void stopUsingGPS(){
            if(locationManager != null){
            locationManager.removeUpdates((LocationListener) GPSTRACKER.this);
            }       
    }

    /**
     * Function to get latitude
     * */
    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     * */
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    //public void showSettingsAlert(){
        //AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
       // alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
       // alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
       // alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
         //   public void onClick(DialogInterface dialog,int which) {
            //  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                //mContext.startActivity(intent);
           // }
       // });

        // on pressing cancel button
       // alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        //    public void onClick(DialogInterface dialog, int which) {
          //  dialog.cancel();
           // }
        //});

        // Showing Alert Message
      //  alertDialog.show();
    //}

    //@Override
    //public void onLocationChanged(Location location) {
    //}

    //@Override
    //public void onProviderDisabled(String provider) {
    //}

    //@Override
    //public void onProviderEnabled(String provider) {
    //}

    //@Override
    //public void onStatusChanged(String provider, int status, Bundle extras) {
    //}

    @Override
    public void onDestroy() {
        super.onDestroy();
        wakeLock.release();

    }

}

那就是我的logcat:

05-11 21:45:53.943: E/Gsm/SmsMessage(15704): hasUserDataHeader : false
05-11 21:45:54.093: E/AndroidRuntime(15848): FATAL EXCEPTION: main
05-11 21:45:54.093: E/AndroidRuntime(15848): java.lang.RuntimeException: Unable to start service com.example.yeah.GPSTRACKER@40555c40 with Intent { cmp=com.example.yeah/.GPSTRACKER (has extras) }: java.lang.NullPointerException
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.ActivityThread.access$2800(ActivityThread.java:117)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.os.Looper.loop(Looper.java:130)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at java.lang.reflect.Method.invokeNative(Native Method)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at java.lang.reflect.Method.invoke(Method.java:507)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at dalvik.system.NativeStart.main(Native Method)
05-11 21:45:54.093: E/AndroidRuntime(15848): Caused by: java.lang.NullPointerException
05-11 21:45:54.093: E/AndroidRuntime(15848):    at com.example.yeah.GPSTRACKER.onStart(GPSTRACKER.java:121)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.Service.onStartCommand(Service.java:428)
05-11 21:45:54.093: E/AndroidRuntime(15848):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
05-11 21:45:54.093: E/AndroidRuntime(15848):    ... 10 more

P.s:继续我的上一个问题:Service doesnt start ! !?

0 个答案:

没有答案