1

Sorry I'm new on android java. Please read the comment tag "//" on the code below, When i clicked my button, the send mail and uninstall is working in same time, how to delay it?

@Override
    public void onClick(View v) {
        // execute send mail first
        sendEmail();
        // delayed 30 second then execute this uninstall.
        Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart"); 

            Intent uninstallIntent =
              new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
        startActivity(uninstallIntent);
    }

3 Answers3

0

You can do:

@Override
    public void onClick(View v) {
        // execute send mail first
        sendEmail();
        Thread.sleep(30000L);// delayed 30 second then execute this uninstall.
        Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart"); 

            Intent uninstallIntent =
              new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
        startActivity(uninstallIntent);
    }

BUT it is not a good development implementation to sleep threads, instead you can refactor some modules and wait for some Callback or notifications.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    The problem here is not so much the delay itself, but that the delay happens on the main UI thread > locking the main UI thread is not good practice as it'll appear to the user as if the app is hanging. – AgentKnopf Sep 12 '17 at 18:55
0

You can use a handler to delay a method for the required time. Here is how:

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Your method to be executed after the delay.
            }
        }, 1000); //1000 is the time in milliseconds( 1 sec) to wait.
drWisdom
  • 502
  • 1
  • 4
  • 12
0

Use postDelayed():

@Override public void onClick(View v) {
        // execute send mail first
        sendEmail();
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                Uri packageUri = Uri.parse("package:com.naufalazkia.zitongart"); 

                Intent uninstallIntent =
                    new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
                    startActivity(uninstallIntent);
            }
         }, 30000L);
    }
daniel.keresztes
  • 875
  • 2
  • 15
  • 25