0

データベースに接続する Android アプリケーションがあります。データベースからデータを継続的に更新するバックグラウンド スレッドを書きたいのですが、その更新されたデータを通知として表示するにはどうすればよいですか? たとえば、スカイプで新しいメッセージを受け取った場合、通知が正しく届きます。

4

1 に答える 1

0

これは、次の方法で実行できます。次のActivityものが含まれている必要があります。

//the method which changes the data in the database
changeDataBD(params) {
    new Thread(new Runnable() { 
        changingDataDB(params);
    }).start();
}

//create an handler
private final Handler myHandler = new Handler();

private void changingDataDB(params) {
    //.... changing of data in DB according to passed params

    //update the UI using the handler and the runnable
    myHandler.post(updateRunnable);

}

final Runnable updateRunnable = new Runnable() {
    public void run() {
        //call the activity method that updates the UI
        updateUIandShowNotification();
    }
};

private void updateUIandShowNotification() {
      // ... update the data on UI
      // ... show a notification
}

データベースの変更を監視するリスナーを作成することもできます。そして、このスレッドを開始する必要があります。

于 2012-05-02T13:18:46.343 に答える