0

我需要将我当前的地理位置(纬度和经度)与我存储在数据库中的地理位置进行比较,如果两者之间的距离小于 2 公里,那么我想开始一些活动..但它不起作用..我是认为我的服务类中存在一些问题,因为我在后台服务中执行所有这些操作,该服务在我开始活动时开始......请告诉我做错了什么这是我的代码

package com.example.alert;

import java.util.ArrayList;
import java.util.List;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class service extends Service{
LocationManager lm;
DatabaseHelper db=new DatabaseHelper(this);
List<Double> arlist=new ArrayList<Double>();
List<Double> arrylist=new ArrayList<Double>();


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreate() {
    LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener ll=new MyLocationListener();
    //lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5*60*10000, 0, ll);
    lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, ll);

//      Toast.makeText(this, "Background Service Created", Toast.LENGTH_LONG).show();



}

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();


}

public void onStart(Intent intent, int startid) {

    Toast.makeText(this, "Finding your Current Location.....", Toast.LENGTH_LONG).show();
//  Intent myIntent=new Intent(service.this,CurrentLocation.class);
    }





/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {
        if(loc!=null)
        {

            String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
            SQLiteDatabase y = db.getWritableDatabase();

            Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
        //  String t=loc.getLatitude().toString();
            Cursor cr=null;
            Cursor cur=null;
            cur=y.rawQuery("SELECT longitude FROM"+ "data",null);
            cur.moveToFirst();
            cr=y.rawQuery("SELECT latitude FROM"+ "data",null);
            cr.moveToFirst();
            while(!cr.isAfterLast()) {

                arlist.add(cr.getDouble(cr.getColumnIndex("latitude")));
                cr.moveToNext();
                }
            while(!cur.isAfterLast()) {

                arrylist.add(cr.getDouble(cr.getColumnIndex("ongitude")));
                cur.moveToNext();
                }
            for(int i=0;i<arlist.size();i++){
                if(calcDistance(loc.getLatitude(),loc.getLongitude(),arlist.get(i),arrylist.get(i))<5){
                    Intent myIntent=new Intent(service.this,SilVib.class);
                    startActivity(myIntent);
                }
            }

            /*  cr.moveToFirst();
            while(!cr.isAfterLast()) {
                 al.add(cr.getString(cr.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
                 cr.moveToNext();
            }*/
        //  lm.addProximityAlert(loc.getLatitude(), loc.getLongitude(), 10009,, intent)

            //for loop, alerters retrieving one by one
//      x= calcDistance(loc.getLatitude(),loc.getLongitude(),z,y);
    //      if(x<1){
        //        Intent myIntent = new Intent(ViewAlerters.this,CallMode.class);
          //        startActivity(myIntent);
            }

        }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getApplicationContext(),"Gps Disabled - @From :Alert Me",Toast.LENGTH_SHORT ).show();


    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getApplicationContext(),"Gps Enabled - @From :Alert Me",Toast.LENGTH_SHORT).show();

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
    }


public double calcDistance(double latA, double longA, double latB, double longB) {

    double theDistance = (Math.sin(Math.toRadians(latA)) *  Math.sin(Math.toRadians(latB)) + Math.cos(Math.toRadians(latA)) * Math.cos(Math.toRadians(latB)) * Math.cos(Math.toRadians(longA - longB)));
    return new Double((Math.toDegrees(Math.acos(theDistance))) * 69.09*1.6093);
}

}

这是我更新的代码...每当我尝试使用意图启动此服务时,它都会强制关闭

package com.example.alert;

import java.util.ArrayList;
import java.util.List;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class service extends Service{
LocationManager lm;
DatabaseHelper db=new DatabaseHelper(this);
List<Double> arlist=new ArrayList<Double>();
List<Double> arrylist=new ArrayList<Double>();
SQLiteDatabase y = db.getWritableDatabase();


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onCreate() {
    LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

    LocationListener ll=new MyLocationListener();
    //lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5*60*10000, 0, ll);
    lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, ll);


}

public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();


}

public void onStart(Intent intent, int startid) {

    Toast.makeText(this, "Finding your Current Location.....", Toast.LENGTH_LONG).show();
//  Intent myIntent=new Intent(service.this,CurrentLocation.class);
    }





/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {
        if(loc!=null)
        {

            String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();

            Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
        //  String t=loc.getLatitude().toString();
            Cursor cr=null;
            Cursor cur=null;
            cur=y.rawQuery("SELECT longitude FROM"+ "data",null);
            cur.moveToFirst();
            cr=y.rawQuery("SELECT latitude FROM"+ "data",null);
            cr.moveToFirst();
            while(!cr.isAfterLast()) {

                arlist.add(cr.getDouble(cr.getColumnIndex("latitude")));
                cr.moveToNext();
                }
            while(!cur.isAfterLast()) {

                arrylist.add(cr.getDouble(cr.getColumnIndex("ongitude")));
                cur.moveToNext();
                }
            for(int i=0;i<arlist.size();i++){
                Location locB = new Location("point B");
                locB.setLatitude(arlist.get(i));
                locB.setLongitude(arrylist.get(i));
                float distance=loc.distanceTo(locB);
                if(distance<5){
                    Intent myIntent=new Intent(service.this,Notifier.class);
                    startActivity(myIntent);

                }
            }
            }

        }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getApplicationContext(),"Gps Disabled - @From :Alert Me",Toast.LENGTH_SHORT ).show();


    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getApplicationContext(),"Gps Enabled - @From :Alert Me",Toast.LENGTH_SHORT).show();

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
    }



}

当我尝试使用 Intent 访问该服务时,这是我的 logcat

11-11 08:22:25.867: E/AndroidRuntime(813): FATAL EXCEPTION: main
11-11 08:22:25.867: E/AndroidRuntime(813): java.lang.RuntimeException: Unable to instantiate service com.example.alert.service: java.lang.NullPointerException
11-11 08:22:25.867: E/AndroidRuntime(813):  at  android.app.ActivityThread.handleCreateService(ActivityThread.java:2347)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.app.ActivityThread.access$1600(ActivityThread.java:130)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.os.Looper.loop(Looper.java:137)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-11 08:22:25.867: E/AndroidRuntime(813):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 08:22:25.867: E/AndroidRuntime(813):  at java.lang.reflect.Method.invoke(Method.java:511)
11-11 08:22:25.867: E/AndroidRuntime(813):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-11 08:22:25.867: E/AndroidRuntime(813):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-11 08:22:25.867: E/AndroidRuntime(813):  at dalvik.system.NativeStart.main(Native Method)
11-11 08:22:25.867: E/AndroidRuntime(813): Caused by: java.lang.NullPointerException
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
11-11 08:22:25.867: E/AndroidRuntime(813):  at com.example.alert.service.<init>(service.java:25)
11-11 08:22:25.867: E/AndroidRuntime(813):  at java.lang.Class.newInstanceImpl(Native Method)
11-11 08:22:25.867: E/AndroidRuntime(813):  at java.lang.Class.newInstance(Class.java:1319)
11-11 08:22:25.867: E/AndroidRuntime(813):  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2344)
4

0 回答 0