0

Can anyone help me on this, I'm in android.

I want to close the apps after 10 mins of idle time and i search the web and found this link:

Application idle time

but i have a little problem with this part of the code:

// soft stopping of thread
public synchronized void stop() {
    stop = true;
}

this error comes out :

Multiple markers at this line - Cannot override the final method from Thread - overrides java.lang.Thread.stop

Please help me understand it.

thanks!

Community
  • 1
  • 1

2 Answers2

2

Seems that stop() is not needed to fix the issue. I would suggest to do it like the following:

public class Waiter extends Thread {
    private static final String TAG=Waiter.class.getName();
    private long lastUsed;
    private long period;
    private boolean stop;
    private final WeakReference<Context> mContextRef;

    public Waiter(final long period, final Context context) {
        this.period = period;
        stop = false;
        mContextRef = new WeakReference<Context>(context);
    }

    public void run() {
        long idle = 0;
        this.touch();

        do {
            idle = System.currentTimeMillis()-lastUsed;
            Log.d(TAG, "Application is idle for " + idle + " ms");
            try {
                Thread.sleep(5000); //check every 5 seconds

                if(idle > period) {
                    final Context context = mContextRef.get();

                    if (context != null) {
                        // start activity
                        startActivity(context);
                    }

                    stop = true;
                }
            }
            catch (InterruptedException e) {
                Log.d(TAG, "Waiter interrupted!");
                // set stop, because smb has called interrupt() on the thread
                stop = true;
            }
        }
        while(!stop);
        Log.d(TAG, "Finishing Waiter thread");
    }

    private void startActivity(final Context context) {
        final Intent intent = new Intent(context, MyActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // If there is nothing that can send a text/html MIME type
            e.printStackTrace();
        }
    }

    public synchronized void touch() {
        lastUsed=System.currentTimeMillis();
    }

    public synchronized void setPeriod(long period) {
        this.period=period;
    }

}

So, it'll be one-shot thread (once idle timeout, You'll need to create new one) which You'll be able to stop any time by just using standard API Thread.interrupt().

sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • Hi, got another question, the code is working very fine, but i just noticed that the rule on my idle time run only once. i want the same rules to run every time i open the apps. where do i have to reset the timer? – zandro zoriano Mar 07 '13 at 21:38
  • Do You mean, that if Your app get closed it's not triggered again? Hm, I see this flow like the following: 1. First activity (e.g. splash screen calls Application class to start the timer); 2. If You exit application (or the user presses home key), then activity might call Application class to stop the timer. – sandrstar Mar 08 '13 at 01:35
  • Yes i want to start the the timer every time i open the apps, but if i press home key it should not stop the timer, the time will only stop if it reach the time, (unless of course the user close the apps), and my another activity open and asking the user to close the application.. this is also the reason why i ask you how to open another intent. – zandro zoriano Mar 08 '13 at 02:03
  • ok, I think You might introduce some kind of activities counter in Application class in order to determine if activity is first one or some activities already presented. If it's the first one, then just instantiate Waiter again and launch it using srtart(). – sandrstar Mar 08 '13 at 02:17
  • okay, with this state how i will going to pass a string to my ControlApplication class? – zandro zoriano Mar 08 '13 at 02:20
  • I'm not sure which string You're talking about actually. However, if You need to pass something, just add it as parameter to some API. And I just noticed, that actually if You're going to restart waiter, then You seems don't need to use Application class. Checkout this question http://stackoverflow.com/questions/15048902/should-initialization-logic-say-for-numerous-singletons-be-in-oncreate-or-onre and comments about singleton realization without Application class usage. – sandrstar Mar 08 '13 at 03:33
  • @ sandrstar I already resolve it, My Global Application now only running when i execute the my splash. my problem now is i want to call the InterruptedException in my Waiter thread to make the thread stop. any idea? – zandro zoriano Mar 08 '13 at 10:23
  • call interrupt() (I provided link to it in my answer, at the end - Thread.interrupt()) – sandrstar Mar 08 '13 at 11:34
1
   public class test extends Activity {

    ImageView _playButton;
    Handler h;
    Runnable r;
    LinearLayout mainLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
        h=new Handler();
        r=new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                finish();       
            }
        };

        mainLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                shutIdleApp();
                return false;
            }
        });



    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        h.removeCallbacks(r);
    }


    public void shutIdleApp()
    {
         h.removeCallbacks(r);
         h.postDelayed(r,10*1000);
    }
}

Here is my implementation for the problem in case people feel uncomfortable playing with the complex code.

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37