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.

Haider
  • 938
  • 2
  • 11
  • 25
  • Can you also post the logcat before it reboots? – SERPRO Jun 18 '12 at 11:08
  • @SERPRO If I run the app while connecting to the computer or while using an app like alogrec, I don't get a reboot. So I cannot provide the logcat. However if there is some other way to find logcat, kindly let me know. – Haider Jun 18 '12 at 13:47
  • you can have a look at this question: http://stackoverflow.com/questions/3469016/can-i-get-logcat-logs-after-phone-reboots it might help you to find out the logcat – SERPRO Jun 18 '12 at 13:57
  • @SERPRO When I am connected to the computer, the application does not cause reboot, don't know why. My question to you is: as an expert in android, do you see anything in the above code that could reboot the phone? I will try to get logs by tomorrow IA and post here. Tomorrow at same time. Plus I tried the app on a samsung phone and it does not cause any reboot there. However in samsung the isMyServiceRunning() function always returns false for some reason. – Haider Jun 18 '12 at 14:17
  • Well, I can see some improvements in your way of detecting running services.. That might be the problem.. try what was suggested in this answer: http://stackoverflow.com/a/608600/571353 – SERPRO Jun 18 '12 at 14:24
  • I copied isMyServiceRunning() function from the same thread. It is the second post with 123 upvotes. But this function is only called when view is rendered or button is pressed. My app reboots phone when it is in sleep / standby mode. – Haider Jun 18 '12 at 14:39
  • There is no way bad code should be able to cause a reboot, its basically a bug in the phone, there might be a way around it though. – mbwasi Jun 18 '12 at 15:35

0 Answers0