0

バックグラウンドサービスで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>
4

3 に答える 3

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()このコードを次の場所に移動して、コンテキストが初期化されるのを待つだけです。

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
于 2012-11-09T23:41:17.913 に答える
1

AndroidManifestファイルにサービスが追加されていることを確認してください

サービスは、ユーザーと対話せずに長時間実行される操作を実行したい、または他のアプリケーションが使用する機能を提供したいというアプリケーションの要望を表すアプリケーションコンポーネントです。各サービスクラスには、パッケージのAndroidManifest.xmlに対応する宣言が含まれている必要があります。サービスは、Context.startService()およびContext.bindService()で開始できます。

于 2012-11-09T23:41:56.220 に答える
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
于 2013-12-11T08:20:36.323 に答える