0

i want to check which app is currently running if it match it show Message. Here is code of my service

public class MyService extends Service{

Context context;
ActivityManager am ;
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo;
Dbheltool db;
String packname;
String key;

HashMap<String, String> list;
public MyService(Context context) {
    // TODO Auto-generated constructor stub4
    this.context=context;
    db=new Dbheltool(context);
}


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

    am = (ActivityManager) getSystemService(context.ACTIVITY_SERVICE);
    runningAppProcessInfo = am
            .getRunningAppProcesses();
    try{
        db.open();
        list=(HashMap)db.getPackage();
        db.close();


    }catch (java.sql.SQLException e) {
        // TODO Auto-generated catch block
        Toast.makeText(context, "Exception", Toast.LENGTH_SHORT);
    }

}
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
    if(isThisApplicationRunning(context)){
        Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
    }




}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

public boolean isThisApplicationRunning(final Context context) {

    for (final RunningAppProcessInfo app : runningAppProcessInfo) {
        try{
            if(list.get(app.processName.toString()).equals(app.processName.toString())) {
                return true
            }
        }
        catch(NullPointerException e){

        }
    }
    return false;
}

}

when i run this app it fail and give Nullpointer Exception and service stops... please help me how to do this. Goal of my Service is...."My service is running in background and checks which app run by user. when ever user click on any app. service check app package it match to the Hashmap object if its find the key and value of hashmap object match the string to packagename of current running application it show toast"

Aamir
  • 141
  • 1
  • 9
  • You could go into more where the error is returned. Is it when you use your isThisApplicationRunning method? – JARRRRG May 30 '14 at 19:53
  • yah i know it gives exception inthe loop i catch exception it but it didnt give me what i want and at the end service stops. – Aamir May 30 '14 at 19:58
  • yes i put package name of apps in 'list' as a key. which is save in my sqlite db and its returns hashmap object... the data store like this...list.put("com.example.app","appname"). – Aamir May 30 '14 at 20:06
  • Have you tried the if statement without .toString()? because they should essentially both be strings anyway right? – JARRRRG May 30 '14 at 20:10
  • yes i check it if statement without .toString() – Aamir May 30 '14 at 20:14
  • hmm I really can't think what could be causing the nullpointer exception. I can only suggest you comment out the method for now and try do System.out.println statements for both your app.processName.toString and list.get(app.processName.toString(). See if they are valid statements. – JARRRRG May 30 '14 at 20:48

1 Answers1

0

The nullpointer looks like it is coming from your line :

   if (list.get(app.processName).equals(app.processName)) {

Your comparison statement seems to be comparing a non existent value?

Also take a look at this answer here by Kkudi : Android: how do I check if activity is running?

It may help you in constructing your method.

Community
  • 1
  • 1
JARRRRG
  • 917
  • 2
  • 14
  • 44