Detecting Currently running App. Android -


i using following code detect running app . when running new app. not getting it's package name, toasting "com.hedy.launcher". whats wrong code ?

 @override           public int onstartcommand(intent intent,int flags, int startid) {                 // todo auto-generated method stub                 super.onstartcommand(intent, flags,startid);                 toast.maketext(this, "service running", toast.length_short).show();                  handler = new handler(){                      @override                     public void handlemessage(message msg)                      {                         // todo auto-generated method stub                         super.handlemessage(msg);                        // count=count+1;                        // toast.maketext(myservice.this, ""+count, toast.length_short).show();                         activitymanager = (activitymanager) getapplicationcontext().getsystemservice(activity.activity_service);                         string packagename = am.getrunningtasks(1).get(0).topactivity.getpackagename();                         toast.maketext(myservice.this, ""+packagename, toast.length_short).show();                     }                  };                    new thread(new runnable(){                     public void run() {                     // todo auto-generated method stub                     while(true)                     {                        try {                         thread.sleep(1000);                         handler.sendemptymessage(0);                      } catch (interruptedexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                       }                                      }                 }).start();                 return 0;             } 

string packagename = am.getrunningtasks(1).get(0).topactivity.getpackagename(); 

this line extracting package name. try increase maxnum parameter(you have set 1).

am.getrunningtasks(maxnum) return arraylist. check contents of arraylist see whether getting required item in of them


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -