服务没有开始! !?

时间:2014-05-11 17:47:47

标签: android service

我试图在后台获取服务,我从BroadcastReceiver调用它...这就是我的服务代码:

public class GPSTRACKER extends Service  {

    private final Context mContext;

    // 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;
    @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)
                    {

                        if(canGetLocation()){

                             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*(-1)>0 && line*(-1)<0.001) || (line>0 && line<0.001)){
                        success=1;
                        break;
                    }
                    }
                    if (success==1){
                    break;
                    }
                        }else{
                            // can't get location
                            // GPS or Network is not enabled
                            // Ask user to enable GPS/network in settings
                            showSettingsAlert();
                            break;
                        }
                    }
                    if (success==1)
                            {
                    Toast poast = Toast.makeText(this, "I FOUND U", Toast.LENGTH_LONG);
                    poast.show();
                            }
                }

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

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


    /**
     * 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();

    }

}

那就是我如何从broadcastReceiver中调用它:

public class SmsListener extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {

            Bundle bundle = intent.getExtras();

            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n < messages.length; n++) {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }


            Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
            toast.show();
            String s=smsMessage[0].getMessageBody();

            Intent intentone = new Intent(context.getApplicationContext(), GPSTRACKER.class);

            intentone.putExtra("message", s);
            context.startService(intentone);

            abortBroadcast();


        }

这就是我的logcat:

05-11 20:19:15.283: E/Gsm/SmsMessage(3677): hasUserDataHeader : false
05-11 20:19:15.408: E/AndroidRuntime(3867): FATAL EXCEPTION: main
05-11 20:19:15.408: E/AndroidRuntime(3867): java.lang.RuntimeException: Unable to instantiate service com.example.yeah.GPSTRACKER: java.lang.InstantiationException: com.example.yeah.GPSTRACKER
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1933)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.app.ActivityThread.access$2500(ActivityThread.java:117)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.os.Looper.loop(Looper.java:130)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.app.ActivityThread.main(ActivityThread.java:3691)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at java.lang.reflect.Method.invoke(Method.java:507)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at dalvik.system.NativeStart.main(Native Method)
05-11 20:19:15.408: E/AndroidRuntime(3867): Caused by: java.lang.InstantiationException: com.example.yeah.GPSTRACKER
05-11 20:19:15.408: E/AndroidRuntime(3867):     at java.lang.Class.newInstanceImpl(Native Method)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at java.lang.Class.newInstance(Class.java:1409)
05-11 20:19:15.408: E/AndroidRuntime(3867):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:1930)
05-11 20:19:15.408: E/AndroidRuntime(3867):     ... 10 more

任何帮助都会有帮助。

0 个答案:

没有答案