1

I'm trying to build an application where my application runs in the background and detects when the user launches any another application.

For an example if user launches Google Play Store then I want to show a Toast "Google Play App has launched"

Here is the SO link, which I am following: https://stackoverflow.com/a/11346800

Whenever I do tap on any of the App like : Google Play App or Gmail App or any other app. I am not getting any Toast message that says "APPNAME app has launched"

What I missed in my code, Here is the complete code of my Service class:

public class NotifyService extends Service {

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

    @Override
    public void onCreate() {
        Toast.makeText(this, " MyService Created ", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, " MyService Started", Toast.LENGTH_LONG).show();
        final int currentId = startId;

        Runnable r = new Runnable() {
            public void run() {

                for (int i = 0; i < 3; i++) {
                    long endTime = System.currentTimeMillis() + 10 * 1000;

                    while (System.currentTimeMillis() < endTime) {
                        synchronized (this) {
                            try {
                                wait(endTime - System.currentTimeMillis());

                                Process mLogcatProc = null;
                                BufferedReader reader = null;
                                mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});

                                reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));

                                String line;
                                final StringBuilder log = new StringBuilder();
                                String separator = System.getProperty("line.separator");

                                while ((line = reader.readLine()) != null)
                                {
                                    log.append(line);
                                    log.append(separator);
                                }
                                String w = log.toString();
                                Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show();

                            } catch (Exception e) {
                            }
                        }
                    }
                    Log.d("ServiceRunning:", "Service Running");

                }
                stopSelf();
            }
        };

        Thread t = new Thread(r);
        t.start();
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
    }

}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(MainActivity.this, NotifyService.class));
    }
}
Community
  • 1
  • 1
Sonali
  • 783
  • 4
  • 11
  • 26
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Prerak Sola Jan 06 '17 at 11:04
  • Check the following link: http://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched – Partha Chakraborty Jan 06 '17 at 11:45
  • Trying to capture logcat output and take some action based on its contents is something which will be flagged by Google play as malicious. Thus can result in your app being blocked/delisted. Also, running logcat like this will not provide it with permissions to read the full system log (it will run with the same security context as your app.) – Larry Schiefer Jan 06 '17 at 13:35

0 Answers0