0

I am trying to create a service that will show a toast every second with the most recent running application. Every time I start the service, I get a NullPointerException. What can I do to avoid this?

public class CheckRunningActivity extends Service {
private static final String TAG = "CheckRunningActivity";
boolean checkApps;
private Timer mTimer = null;
private Handler mHandler = new Handler();
private ActivityManager am;

public static final long NOTIFY_INTERVAL = 1000; // 1 second

    @Override
    public void onDestroy() {
        mTimer.cancel();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

@Override
public void onCreate() {
    Log.d(TAG, "I created it");   

     ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

     // cancel if already existed
    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}



class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                Log.d(TAG, "Its running");   

        String packageName = am.getRunningTasks(1).get(0).topActivity
                .getPackageName();

            Toast.makeText(getBaseContext(), packageName, Toast.LENGTH_LONG).show();
            Log.d(TAG, "Make Toast");           
            }
        });
     }
 }          

}

Trent Pierce
  • 387
  • 3
  • 22
  • 1
    try cehcking for null before doing `mTimer.cancel();` in `onDestroy()`. – faizal Jul 03 '14 at 08:09
  • your original comment was correct. It was a silly mistake. I just overlooked my AM initialization. Thank you for suggesting that I recheck it. – Trent Pierce Jul 03 '14 at 08:10
  • your welcome. i deleted my original comment after seeing the initialization in the edited question :) – faizal Jul 03 '14 at 08:11

2 Answers2

0

you can't directly call toast from service . you need a handler for this. see answer here.

Community
  • 1
  • 1
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

after looking at the code again, I realized that I had not properly initialized the Activity Manager.

Here is the corrected code...

public class CheckRunningActivity extends Service {
private static final String TAG = "CheckRunningActivity";
boolean checkApps;
private Timer mTimer = null;
private Handler mHandler = new Handler();
private ActivityManager am;

public static final long NOTIFY_INTERVAL = 1000; // 1 second

    @Override
    public void onDestroy() {
        mTimer.cancel();
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

@Override
public void onCreate() {
    Log.d(TAG, "I created it");   


     // cancel if already existed
    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}



class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                Log.d(TAG, "Its running");   

        String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

            Toast.makeText(getBaseContext(), packageName, Toast.LENGTH_LONG).show();
            Log.d(TAG, "Make Toast");           
            }
        });
     }
 }          
}   
Trent Pierce
  • 387
  • 3
  • 22