14

通知を作成し、特定の情報で定期的に更新するサービスがあります。約 12 分後、電話がクラッシュして再起動します。通知を更新する方法に関連する次のコードのメモリ リークが原因であると思われます。間違っています。

作成時:

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

createNotification:

private void createNotification() {
  Intent contentIntent = new Intent(this,MainScreen.class);
  contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent appIntent =PendingIntent.getActivity(this,0, contentIntent, 0);

  contentView = new RemoteViews(getPackageName(), R.layout.notification);
  contentView.setImageViewResource(R.id.image, R.drawable.icon);
  contentView.setTextViewText(R.id.text, "");

  notification = new Notification();
  notification.when=System.currentTimeMillis();
  notification.contentView = contentView;
  notification.contentIntent = appIntent;
}

更新通知:

private void updateNotification(String text){
  contentView.setTextViewText(R.id.text, text);
  mNotificationManager.notify(0, notification);
}

前もって感謝します。

4

2 に答える 2

9

私は同じ問題に遭遇しました。RemoteView と Notification をサービスに「キャッシュ」せずに、「更新」ルーチンで最初から再作成すると、この問題は解決するようです。はい、効率的ではないことはわかっていますが、少なくとも電話はメモリ不足エラーから再起動しません.

于 2010-12-19T19:30:15.827 に答える
2

私はまったく同じ問題を抱えていました。私の解決策は@haimgが言ったものに近いですが、通知をキャッシュします(RemoteViewだけが再作成されます)。そうすることで、通知を見ているときに通知が再度点滅することはありません。

例:

public void createNotification(Context context){
    Notification.Builder builder = new Notification.Builder(context);

    // Set notification stuff...

    // Build the notification
    notification = builder.build();
}

public void updateNotification(){
    notification.bigContentView = getBigContentView();
    notification.contentView = getCompactContentView();

    mNM.notify(NOTIFICATION_ID, notification);
}

メソッドgetBigContentViewとIでは、更新されたレイアウトgetCompactContentViewで new を返します。RemoteViews

于 2013-05-09T18:04:47.467 に答える