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"