だから私はサービスを作成しました、そしてそれはうまくいきます。カウントするだけです。通知を使用する手間を処理するカスタム クラスがあります。このクラスには、明らかに使用する通知を返す getNotification があります。すべてがうまく機能しましたが、サービスをフォアグラウンドで実行する必要があります (アプリが終了するまで中断してはならない重要なデータをアプリに同期します) 今、startForeground を追加するときに、通知と ID を追加します。このように残します。startForeground(1337、通知);
私が抱えている問題は、私が最初に行う notify() が、何らかの理由で他の通知から独立していることです。したがって、これを実行すると、2 つの通知が作成されます。最初のものは、「Zilean」というタイトルの最初のアップデートで立ち往生しており、コンテンツには「Counting」と書かれています。もう1つは完全に更新されます。startForeground を ID 0 (startForeground(0,notification)) で実行すると、この問題は修正されますが、アクティビティを強制終了すると通知が停止することに気付きました。id <> 0 の場合は発生しません。
私はこの問題をあまりにも長く抱えていたので、カウントするだけのダミーサービスを使用しています. ユーザーが原因でアクティビティが終了した場合、または単に Android がメモリの削除を決定したためにアクティビティが終了した場合、サービスは継続されることを知りたいです。
// onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// NOTIFICATION SECTION
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
notificationManagerClass = new SBNNotification(mNotifyManager,
mBuilder, true, 1, false, "Zilean",
"Initiating Counter", false);
notificationManagerClass.notificate();
final Notification notification = notificationManagerClass.getNotification();
notification.flags = flags;
notification.defaults = Notification.DEFAULT_LIGHTS;
startForeground(1337, notification);
new Timer().scheduleAtFixedRate(new TimerTask () {
int i=0;
@Override
public void run() {
notificationManagerClass.setContentTitle("Contando");
notificationManagerClass.setContentText(String.valueOf(i));
notificationManagerClass.notificate();
i++;
Log.e("HOLAAA", String.valueOf(i));
}}, 0, 1000);
return START_REDELIVER_INTENT;
}
だから..何が欠けているのですか?