1

I have created a very basic program to track GPS coordinates. The program has a button that turns a service on and off. The service simple installs a LocationListener. On each onLocationChanged the location coordinates are dumped on sd card.

The application works fine. But when I push the sleep button, the application logs data for sometime and then causes Google Nexus One to reboot.

Below I am posting the MINIMUM code that is required to reproduce this bug. I have striped extra code to avoid confusion.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phonetracker"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PhoneTrackerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyGPSService" android:process=":my_gps_service" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
     android:id="@+id/on_button_click" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/on_button_click"
     android:onClick="onButtonClick" />

</LinearLayout>

values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, PhoneTrackerActivity!</string>
    <string name="app_name">PhoneTracker</string>
    <string name="on_button_click">Start Service</string>

</resources>

PhoneTrackerActivity.java

package com.phonetracker;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class PhoneTrackerActivity extends Activity
{
    private static final String TAG = "MyGPSServiceDemoActivity";
    private Intent intent           = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.on_button_click);

        if (isMyServiceRunning())
        {
            button.setText("Stop Service");
        }
        else
        {
            button.setText("Start Service");
        }

        if (intent == null)
            intent = new Intent(this, MyGPSService.class);
    }

    public void onButtonClick(View view)
    {
        Log.e(TAG, "onButtonClick");

        Button button = (Button)view;

        if (isMyServiceRunning())
        {
            stopService(intent);
            button.setText("Start Service");
        }
        else
        {
            startService(intent);
            button.setText("Stop Service");
        }
    }

    private boolean isMyServiceRunning()
    {
        Log.e(TAG, "isMyServiceRunning");

        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
        {
            if ("com.gpsservicedemo.MyGPSService".equals(service.service.getClassName()))
            {
                return true;
            }
        }
        return false;
    }
}

MyGPSService.java

package com.phonetracker;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;

public class MyGPSService extends Service
{
    private static final String TAG                     = "Hx2MyGPSService";
    private LocationManager locationManager             = null;
    private GPSLocationListener gpsLocationListener     = null;

    @Override
    public IBinder onBind(Intent arg0)
    {
        Log.e(TAG, "onBind");

        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e(TAG, "onStartCommand");

        super.onStartCommand(intent, flags, startId);

        return START_STICKY;
    }

    @Override
    public void onCreate()
    {
        Log.e(TAG, "onCreate");

        locationManager     = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        gpsLocationListener = new GPSLocationListener();
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
        }
    }

    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");

        super.onDestroy();

        if (locationManager != null)
        {
            try
            {
                locationManager.removeUpdates(gpsLocationListener);
            }
            catch (Exception ex)
            {
                Log.e(TAG, "fail to remove location listners, ignore", ex);
            }
        }

        gpsLocationListener.closeOperations();
    }
}

GPSLocationListener.java

package com.phonetracker;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

public class GPSLocationListener implements LocationListener
{
    private static final String TAG = "Hx2GPSLocationListener";

    public GPSLocationListener ()
    {
        Log.e(TAG, "GPSLocationListener");
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged");
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled");
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged");
    }

    public void closeOperations()
    {
        Log.e(TAG, "closeOperations");
    }
}

Please see if I am missing out something.

4

0 回答 0