1

销毁应用程序已解决!感谢帮助!现在活动被破坏,但状态图标仍然出现在状态栏上。你能在应用程序关闭的同时帮我删除图标吗?我怀疑 onDestroy 部分的问题...


我有一个带有状态栏图标的简单应用程序。当 Activity 启动并且用户按下 onPause() 时,状态栏图标出现。单击此图标恢复应用程序,但是当我想要销毁活动时,我遇到了错误。有人可以帮我解决这个问题吗?

private static final int NOTIF_ID = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jajko);

    text = (TextView) findViewById(R.id.tvTime);
    play = (Button) findViewById(R.id.butStart);
    miekko = (Button) findViewById(R.id.butMiekko);
    srednio = (Button) findViewById(R.id.butSrednio);
    twardo = (Button) findViewById(R.id.butTwardo);

    miekko.setOnClickListener(this);
    srednio.setOnClickListener(this);
    twardo.setOnClickListener(this);
    play.setOnClickListener(this);

    mp = MediaPlayer.create(Jajko.this, R.raw.alarm);

    showNotification(this);
}


    public static void showNotification(Context context) {
        final Intent result_intent = new Intent(context, Jajko.class);

        result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);              

        TaskStackBuilder stack_builder = TaskStackBuilder.create(context);
        stack_builder.addParentStack(Jajko.class);
        stack_builder.addNextIntent(result_intent);


        PendingIntent pending_intent = stack_builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(context);

        Resources res = context.getResources();
        builder.setContentIntent(pending_intent)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icon))
            .setTicker("test")
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(false)
            .setContentTitle("title")
            .setContentInfo("cinfo")
            .setContentText("ctext");
        Notification n = builder.build();
        n.flags = Notification.FLAG_ONGOING_EVENT; 

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(NOTIF_ID, n);     
    }

public void onDestroy() {
    try {
        mp.release();
        if (isFinishing()) {
            notificationManager.cancel(NOTIF_ID);
        }
    } catch (Exception e) {
    }
    super.onDestroy();

}
4

2 回答 2

0

你有NullPointerException方法:

public void onDestroy() {
  mp.release();
if (isFinishing()) {
    notificationManager.cancel(NOTIF_ID);
}
super.onDestroy();

}

在您调用之前mp.release();验证它不是null

我会这样写:

 public void onDestroy() {

  if(mp != null){
      mp.release();
  }

if (isFinishing()) {
    notificationManager.cancel(NOTIF_ID);
}
super.onDestroy();

}

但是,您也可以进行相同的验证 notificationManager

于 2013-09-30T06:52:30.523 回答
0

在这种情况下,必须调用 super.onDestroy 来销毁活动,因为它是 android 生命周期的一部分。

所以你可以把你的代码放在 try/catch 中,它会起作用。

public void onDestroy() {
        try {
            mp.release();
            if (isFinishing()) {
                notificationManager.cancel(NOTIF_ID);
            }
        } catch (Exception e) {
        }
        super.onDestroy();
    }
于 2013-09-30T07:01:29.093 回答