无法运行后台服务

时间:2012-11-09 23:38:44

标签: android service location background-process

我正在尝试在后台服务中使用LocationListener类来每次接收用户的当前位置,然后将其与存储在我的数据库中的某些数据进行比较。但是当我试图这样做时,我得到了错误

    package com.example.alert;

   import android.app.Service;
   import android.content.Context;
   import android.content.Intent;
   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 = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

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

public void onCreate() {
    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, "My Service Started", Toast.LENGTH_LONG).show();
    Intent myIntent=new Intent(service.this,CurrentLocation.class);
    startActivity(myIntent);
    }
}

这是我的logcat

11-10 04:54:54.819: E/AndroidRuntime(768): FATAL EXCEPTION: main
11-10 04:54:54.819: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.alert/com.example.alert.service}: java.lang.NullPointerException
  11-10 04:54:54.819: E/AndroidRuntime(768):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
  11-10 04:54:54.819: E/AndroidRuntime(768):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-10 04:54:54.819: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
11-10 04:54:54.819: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
11-10 04:54:54.819: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-10 04:54:54.819: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-10 04:54:54.819: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
11-10 04:54:54.819: E/AndroidRuntime(768): Caused by: java.lang.NullPointerException
11-10 04:54:54.819: E/AndroidRuntime(768):  at android.content.ContextWrapper.getSystemService(ContextWrapper.java:416)
11-10 04:54:54.819: E/AndroidRuntime(768):  at com.example.alert.service.<init>(service.java:15)
11-10 04:54:54.819: E/AndroidRuntime(768):  at java.lang.Class.newInstanceImpl(Native Method)
11-10 04:54:54.819: E/AndroidRuntime(768):  at java.lang.Class.newInstance(Class.java:1319)

这是位置监听器的其余代码

    package com.example.alert;

   import android.app.Activity;
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
 import android.widget.Toast;

 public class CurrentLocation extends Activity {
LocationManager lm ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    /* Use the LocationManager class to obtain GPS locations */
    LocationListener ll = new MyLocationListener();
    //lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5*60*10000, 0, ll);
    lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, ll);

}



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

    public void onLocationChanged(Location loc)
    {
        if(loc!=null)
        {
            double x;
            String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
            Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();    
            //for loop, alerters retrieving one by one
            //x= calcDistance(loc.getLatitude(),loc.getLongitude(),,);
            //if(x<1)
            // distance < 1, retrieve data from DB and trigger 
        }
    }


    public void onProviderDisabled(String provider)
    {
        Toast.makeText( getApplicationContext(),"Gps Disabled - @From :Alert Me",Toast.LENGTH_SHORT ).show();
    }


    public void onProviderEnabled(String provider)
    {
        Toast.makeText( getApplicationContext(),"Gps Enabled - @From :Alert Me",Toast.LENGTH_SHORT).show();
    }


    public void onStatusChanged(String provider, int status, Bundle extras)
    {

    }

}/* End of Class MyLocationListener */

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);
}


}

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alert"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Instructions" android:label="Instructions"></activity>
    <activity android:name=".AboutApp" android:label="About ALERT ME!"></activity>

    <activity android:name=".ViewAlerters" android:label="Alerters Active"></activity> 
    <activity android:name=".AddAlerter" android:label="Add an Alerter"></activity>
    <activity android:name=".EditAlerter" android:label="Edit an alerter"></activity> 
    <activity android:name=".AddLocation" android:label="Add Location to Alerter"></activity>
    <activity android:name=".AddTrigger" android:label="Add trigger to Alerter"></activity>


    <activity android:name=".SilVib" android:label="Silent/Vibrate Trigger"></activity>
    <activity android:name=".CallMode" android:label="Call Mode Trigger"></activity>
    <activity android:name=".service"></activity>
    <activity android:name=".Status_Notify"></activity>
    <activity android:name=".Notifier"></activity>    
    <activity android:name=".Message"></activity>
    <activity android:name=".Message_sent"></activity>
    <activity android:name=".StoringEntire"></activity>
    <activity android:name=".CurrentLocation"></activity>


    <uses-library android:name="com.google.android.maps"/>
<service
    android:enabled="true"
 android:name=".service"/>
      </application>
 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE"/>


</manifest>

3 个答案:

答案 0 :(得分:2)

LogCat声明:

Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getSystemService(ContextWrapper.java:416)
    at com.example.alert.service.<init>(service.java:15)

只需等待通过将此代码移动到onCreate()

来初始化Context
public class service extends Service {
    LocationManager lm;

    @Override
    public void onCreate() {
        // This line needs an existing context
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Toast.makeText(this, "Background Service Created", Toast.LENGTH_LONG).show();
    }

    // etc

答案 1 :(得分:1)

确保您已将服务添加到AndroidManifest文件

  

服务是一种应用程序组件,表示应用程序希望在不与用户交互的情况下执行较长时间运行的操作,或者为其他应用程序提供要使用的功能。每个服务类必须在其包的AndroidManifest.xml中具有相应的声明。可以使用Context.startService()和Context.bindService()启动服务。

答案 2 :(得分:0)

将此代码用于我的朋友并根据您的需要进行更改

public class MainActivity extends Activity {
    Button btnStart, btnStop;
    public static Context mContext;
    Intent it;
    public LocationManager manager;
    public boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        isGPSEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = manager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                /*if (isGPSEnabled) {
                    startService(getIntent());
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Please oN Gps ",
                            Toast.LENGTH_LONG).show();
                }*/
                startService(getIntent());
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                stopService(getIntent());
            }
        });
    } // on create ends

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public Intent getIntent() {
        it = new Intent(mContext, MyService.class);
        return it;
    } // method ends

    public static Context getContextForService() {
        return mContext;
    } // methods ends
} // final class ends

以下是您的位置服务

public class MyService extends Service {
    // flag for GPS status
    public static LocationManager manager;
    public static boolean isGPSEnabled = false;
    public static double lattitude = 0;
    public static double longitude = 0;
    int count = 0;
    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;
    MyFile myFile;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent, flags, startId);
        System.out.println("On Start Command is called ");
        return START_STICKY;
    } // method ends

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("Service is created//////////// ");
        // start asyntask to get locations

        new GetLocations().execute();
    }// on create ends

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.out.println("Service is  Destroyed  //////////// ");
        if (manager != null && isGPSEnabled == true) {
            manager.removeUpdates(mylistener);
            System.out.println("Service is  Destroyed under if //////////// ");
        }

    } // method ends

    public class GetLocations extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            // getting GPS status
            isGPSEnabled = manager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            // getting network status
            isNetworkEnabled = manager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (isGPSEnabled) {
                /*
                 * Criteria criteria = new Criteria(); String bestProvider =
                 * manager.getBestProvider(criteria, false); Location location =
                 * manager.getLastKnownLocation(bestProvider); double lat
                 * =location.getLatitude(); double longi
                 * =location.getLongitude();
                 * System.out.println("getting location continous ////// Lattti "
                 * +location.getLatitude() );
                 * System.out.println("getting location continous ////// LONGITU "
                 * + location.getLongitude());
                 */
                manager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 3000, 0, mylistener);

            } else {
                Toast.makeText(MyService.this, "Please oN Gps ",
                        Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            // getting current lattitude and longitude

            return null;
        }// method ends
    }// asyntask class ends

    void handleLocationChanged(Location loc) {
        lattitude = loc.getLatitude();
        longitude = loc.getLongitude();
        /*Toast.makeText(MyService.this,
                "lat is " + lattitude + " long is  " + longitude,
                Toast.LENGTH_LONG).show();*/
        System.out.println("getting location continous ////// Lattti "
                + lattitude);
        System.out.println("getting location continous ////// LONGITU "
                + longitude);

        generateNoteOnSD("GpsTesting.txt", "lattitude is" + lattitude + "\n"
                + " longitude" + longitude);

    } // method ends

    public LocationListener mylistener = new LocationListener() {

        @Override
        public void onLocationChanged(Location loc) {
            handleLocationChanged(loc);
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
            // Toast.makeText(mContext, "Gps is disable",
            // Toast.LENGTH_SHORT).show();
        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
            // Toast.makeText(mContext, "Gps is on", Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub
            // Toast.makeText(appcontext, "Gps  status is chnged ",
            // Toast.LENGTH_SHORT).show();
        }
    };

    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                MainActivity.getContextForService());

        // Setting Dialog Title
        alertDialog.setTitle("Alert");

        // 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);
                        getApplicationContext().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();
    }// method ends
        // method for sending notiifcation

    public void sendNotification() {
        int MY_NOTIFICATION_ID = 1;
        PendingIntent contentIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, new Intent(MyService.this,
                        MainActivity.class), 0);
        Notification myNotification = new NotificationCompat.Builder(
                getApplicationContext())
                .setContentTitle("Notification for Lat and Long")
                .setContentText("hi testing notiifaction on lat and long")
                .setTicker(" ToDoList Notification")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(contentIntent)
                .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_launcher).build();
        NotificationManager notificationManager = (NotificationManager) MainActivity
                .getContextForService().getSystemService(
                        Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    } // method ends
        // writing file on

    public void generateNoteOnSD(String sFileName, String sBody) {
        try {
            File root = new File(Environment.getExternalStorageDirectory()
                    + "/sdcard", "Gps_Data");
            if (!root.exists()) {
                root.mkdirs();
            }
            File gpxfile = new File(root, sFileName);
            FileWriter writer = new FileWriter(gpxfile);
            writer.append(sBody);
            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();

        }
    } // method ends

    private boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }
} // final class ends