1

I am currently trying to implement an app that has a service running until the user explicitly ends it via the app.

The service runs even when I exit the app(which is ok)

enter image description here

But when I enter the app again and try to stop the service, the app stops running.

What am I doing wrong?

This is the service code

    package com.mac.service;

    import android.app.Service;
    import android.content.Intent;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Handler;
    import android.os.IBinder;
    import android.widget.Toast;

    public class MyService extends Service {

        private Handler handler = new Handler();
        private long timeAndSpace = 5000;
        Thread t;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            handler.removeCallbacksAndMessages(null);
            Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStart(Intent intent, int startid) {
        }

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

            Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
                    .show();

            final Runnable r = new Runnable() {
                public void run() {
                    Location l = getMine();
                    Toast.makeText(
                            getApplicationContext(),
                            String.valueOf(l.getLatitude()) + " "
                                    + String.valueOf(l.getLongitude()),
                            Toast.LENGTH_SHORT).show();
                    handler.postDelayed(this, timeAndSpace);
                }
            };
            handler.postDelayed(r, timeAndSpace);
            return START_STICKY;
        }

        private Location getMine() {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = locationManager.getBestProvider(criteria, true);
            Location myLocation = locationManager.getLastKnownLocation(provider);
            return myLocation;
        }

    }

and this the manifest

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />

    <uses-permission android:name="com.mac.maps2demo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- Requires OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

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

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

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <service
            android:name="com.mac.service.MyService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>
Marco Aviles
  • 5,516
  • 6
  • 33
  • 47
  • What does "the app stops running" mean? – CommonsWare Oct 26 '14 at 23:24
  • http://i.imgur.com/kKuE2NW.png when I re-enter the app "it stop"(the toast does not appear anymore) – Marco Aviles Oct 26 '14 at 23:40
  • When you looked in LogCat for a stack trace, what did you find? http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Oct 26 '14 at 23:41
  • a null pointer exception Caused by: java.lang.NullPointerException at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1810) it seems can't find the service. – Marco Aviles Oct 26 '14 at 23:44

0 Answers0