我想在開啟一個intent 10秒後執行一個方法:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(GOOGLE_VOICE_SEARCH_PACKAGE_NAME);
startActivity(launchIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
SonrLog.d(TAG, "TIMEOUT, reconnecting!");
reconnectSONR();
}
}, 10000); //10 second timeout
Intent啟動後,代碼執行過 Handler後,沒有任何東西打印出來,也沒有別的方法被調用。
如何在預定時間後,執行java方法呢?
看一下 log日志:Log.d(TAG, "TIMEOUT, reconnecting!");
我使用的下面的代碼:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(GOOGLE_VOICE_SEARCH_PACKAGE_NAME);
startActivity(launchIntent);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "TIMEOUT, reconnecting!");
}
}, 10000); //10 second timeout
}
});
注意: GOOGLE_VOICE_SEARCH_PACKAGE_NAME 是指向其它一些包,因為它只用於測試。