0

For the android application i'm making, I want there to be a service that is checking the a database for an attack even when the app isnt running (kinda like how facebook will notify something even if it's closed) and so i made the class below, yet a minute after i stop my app from running i get a message "App has unexpetedly stopped" and then again a few minutes later...i know its possible to keep checking somehow cause many apps do it but what am i doing wrong? This is what i have so far:

    package com.ducknoise.toonfight;

import java.sql.SQLException;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.widget.Toast;

public class ToonService extends Service{
    public static ToonService toonService;
    public ToonService(){
        toonService = this;
    }
    @Override
    public IBinder onBind(Intent intent) {
        String toonName = intent.getStringExtra("toonName");
        ToonDB task = new ToonDB();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toonName);
        } else {
            task.execute(toonName);
        }
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String toonName = intent.getStringExtra("toonName");
        ToonDB task = new ToonDB();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toonName);
        } else {
            task.execute(toonName);
        }
        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "Disconnected with Server", Toast.LENGTH_LONG).show();
        Database db = new Database();
        db.closeConnection();
        super.onDestroy();
    }

    public class ToonDB extends AsyncTask<String,Void, Void>{

        Toon toon;
        @Override
        protected Void doInBackground(String... params) {
            Database db = new Database();
            db.establishConnection();
            toon = db.getToonFromDB(params[0]);

            return null;

        }
        @Override
        protected void onPostExecute(Void result) {
            ToonCheck task = new ToonCheck();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toon);
            } else {
                task.execute(toon);
            }
            super.onPostExecute(result);
        }

    }
    public class ToonCheck extends AsyncTask<Toon,Void, Void>{

        boolean runCheck;
        Toon toon;
        @Override
        protected Void doInBackground(Toon... params) {
            runCheck = true;
            Database db = new Database();
            db.establishConnection();
            toon = params[0];
            toon.updateCheckDB();
            publishProgress();
            if(!db.isOnline(toon))
                runCheck = false;

            return null;

        }
        @Override
        protected void onProgressUpdate(Void... values) {
            Battle battle = toon.getBattle();
            if(battle != null ){
                if(battle.notified()){
                    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext());
                    nBuilder.setContentTitle("Toon Fight");
                    String contentText = battle.getOppName() + " has attacked you!";
                    nBuilder.setContentText(contentText);
                    nBuilder.setSmallIcon(R.drawable.ic_launcher);
                    int notiID = 1295;
                    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                    notificationManager.notify(notiID, nBuilder.build());
                }
            }
            super.onProgressUpdate(values);
        }
        @Override
        protected void onPostExecute(Void result) {
            if(runCheck){
                ToonCheck task = new ToonCheck();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,toon);
                } else {
                    task.execute(toon);
                }
            }else 
                stopSelf();
            super.onPostExecute(result);
        }

    }
}

this is the logcat

03-24 11:01:41.968: D/AndroidRuntime(4624): Shutting down VM
03-24 11:01:41.968: W/dalvikvm(4624): threadid=1: thread exiting with uncaught exception (group=0x41f5aac8)
03-24 11:01:41.968: E/AndroidRuntime(4624): FATAL EXCEPTION: main
03-24 11:01:41.968: E/AndroidRuntime(4624): java.lang.RuntimeException: Unable to start service com.ducknoise.toonfight.ToonService@430a3950 with null: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.app.ActivityThread.access$2000(ActivityThread.java:152)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1385)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.os.Looper.loop(Looper.java:137)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.app.ActivityThread.main(ActivityThread.java:5329)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at java.lang.reflect.Method.invoke(Method.java:511)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at dalvik.system.NativeStart.main(Native Method)
03-24 11:01:41.968: E/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624):     at com.ducknoise.toonfight.ToonService.onStartCommand(ToonService.java:34)
03-24 11:01:41.968: E/AndroidRuntime(4624):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2765)
03-24 11:01:41.968: E/AndroidRuntime(4624):     ... 10 more

This is how i'm calling the service:

Intent toonService = new Intent(MainActivity.this, ToonService.class);
                toonService.putExtra("toonName", toon.getName());
                MainActivity.this.startService(toonService);
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3293629
  • 119
  • 1
  • 8

1 Answers1

0

You should check if your intent at onStartCommand is null or not per your logcat:

03-24 11:01:41.968: E/AndroidRuntime(4624): Caused by: java.lang.NullPointerException
03-24 11:01:41.968: E/AndroidRuntime(4624): at
com.ducknoise.toonfight.ToonService.onStartCommand(ToonService.java:34)

And please show how do you start Service

nikis
  • 11,166
  • 2
  • 35
  • 45
  • i just posted how i call the service, why would it be null/how could i make it not null? – user3293629 Mar 24 '14 at 15:06
  • @user3293629 it's a feature of `START_STICKY` flag after process being killed. Try to use `START_REDELIVER_INTENT` instead, take a look here http://developer.android.com/reference/android/app/Service.html – nikis Mar 24 '14 at 16:45
  • but wont the STAR_REDILIVER_INTENT lead to the service being closed by the system? I want to close it myself – user3293629 Mar 24 '14 at 16:51
  • @user3293629 no, it's OK to use this flag – nikis Mar 24 '14 at 16:59