在Activity / App进入后台后60秒调用方法

时间:2018-06-03 07:33:45

标签: java android

我希望在将文字放到Android剪贴板上60秒后清除剪贴板。

我尝试了下面的处理程序postDelayed。问题是当活动被杀死或应用程序进入后台(单击主页按钮)时,此功能不会执行。如何确保在应用程序处于后台时调用该功能?

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
           //Clear clipboard
        }
    }, 60000);

1 个答案:

答案 0 :(得分:1)

由于您的活动在应用程序被杀后不会持续存在,因此您必须使用Service类。在某些情况下,它也可能被杀死,但在正常情况下,它会在大多数情况下持续存在(否则检查前台服务)。

这种方法应该有效:

<强>爪哇

public class TestClipboardServiceActivity extends Activity {
    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent(this, MyClipboardService.class));

        // Forced the app to be destroyed intentionally.
        finish();
    }
}

/**
 * Clipboard Service. It will clear the clipboard after 60 seconds.
 */
public class MyClipboardService extends Service {

    public class LocalBinder extends Binder {
        MyClipboardService getService() {
            return MyClipboardService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("MyClipboardService", "Received start id " + startId + ": " + intent);

        // Remember, this is executed in the main thread.
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                clearClipboard();
            }
        }, 6000);

        return START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    /**
     * Clears the clipboard.
     */
    private void clearClipboard() {
        ClipboardManager myClipboard =
            (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        if (myClipboard != null) {
            myClipboard.setPrimaryClip(ClipData.newPlainText(null, "");
        }
    }
}

<强>科特林

class TestClipboardServiceActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    startService(Intent(this, MyClipboardService::class.java))

    // Forced the app to be destroyed intentionally.
    finish()
  }
}

/**
 * Clipboard Service. It will clear the clipboard after 60 seconds.
 */
class MyClipboardService : Service() {

  private val mBinder = LocalBinder()

  inner class LocalBinder : Binder() {
    internal val service: MyClipboardService
      get() = this@MyClipboardService
  }

  override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    Log.i("MyClipboardService", "Received start id $startId: $intent")

    // Remember, this is executed in the main thread.
    Handler().postDelayed(this::clearClipboard, 60000)

    return START_STICKY
  }

  override fun onBind(intent: Intent) = mBinder

  /**
   * Clears the clipboard.
   */
  private fun clearClipboard() {
    val myClipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    myClipboard.primaryClip = ClipData.newPlainText(null, "")
  }
}

另一方面,如果您更喜欢使用自定义库来处理作业,我强烈建议使用Evernote中的android-job。它内部甚至使用最近的WorkManager类,并且该库包含多种调度变体,除了兼容许多Android API和缺少Google Play服务库。在您的情况下,您可以查看setExact(long exactInMs)方法,在作业从计划作业开始运行时提供偏移量。