我正在嘗試創建指向主頁並激活主頁屏幕的活動,並且「數據庫現在已更新」。但是,我不知道如何在收到意圖時才吐氣。但我不知道如何很好地收到意圖,這裏是我的代碼。Android:意圖收到,如果 - 其他
public class MainActivity extends Activity {
TextView time;
Button viewStock, descriptionSearch;
String lastUpdateStandard = "Last Update Time: ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewStock = (Button) findViewById(R.id.detailSearchButton);
descriptionSearch = (Button) findViewById(R.id.descriptionSearchButton);
time = (TextView) findViewById(R.id.time);
// retrieve time from database to see last update time
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lastUpdateTime = db.getLastUpdate();
time.setText(lastUpdateStandard + lastUpdateTime.get(0));
Intent receivedIntent = getIntent();
/**
*
* What should I write if there is no received Intent?
* Probably I need an if-else statement, but I have no idea how it should be made.
*/
}
您的設計是錯誤的。您正在主UI線程中調用數據庫操作。在某些情況下,此操作可能會很長,您的應用程序可能會被操作系統用ANR殺死。 – greywolf82
因此,如果我想從數據庫中獲取某些內容並在頁面啓動時顯示出來,我該怎麼辦?如果這是.MainActivity類? – CHANist
使用單獨的線程,intentservice,asyncTask或任何你想要的,但避免在主線程上執行數據庫操作。在這種情況下,也許一個簡單的asynctask就可以。 – greywolf82