0

大規模システムのプロトタイプ アプリケーションを構築しています。このプロトタイプはオフラインになりますが、サーバーから情報を取得しているように見えます。アプリが開いていない場合でも (DeamonThread を使用)。

そこで、Android アプリケーションを作成し、タスクを作成および削除する AI を (アプリ内に) 追加しようとしています。動作しますが、スレッドがアクティビティではないため、DeamonThread から通知を追加しようとしても機能しません。

extends Activity implements Runnable に変更しようとしましたが、Deamon にすることはできません。

簡単なものが欠けているような気がします..

public void run() {
    while (counter < 100) {
        try {
            sleep(random.nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Task task = new Task("AI", "this was the " + counter
                + " AI message", flow);
            sendNotation();
        }
        counter++;
    }
}

private void sendNotation() {
    NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, Flippin.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    String body = "This is a message from Adam";
    String title = "One new Task";
    NotificationCompat.Builder n = new NotificationCompat.Builder(this);
    n.setContentIntent(pi);
    n.setSmallIcon(R.drawable.notif);
    n.setContentTitle(title);
    n.setContentText(body);
    n.setDefaults(Notification.DEFAULT_ALL);
    n.setAutoCancel(true);
    nm.notify(uniqueID, n.build());
    finish();       
}
4

2 に答える 2

0

デーモンを開始したい場合は、サービスを確認する必要があります

サービスから通知を送信するためのチュートリアルは数多くあります。

はい、 Handlerを使用して、UI 以外のスレッドから通知を送信することは可能です。

于 2013-07-30T13:25:53.640 に答える