【问题标题】:Status bar icon can't remove after destroy销毁后状态栏图标无法删除
【发布时间】:2013-11-06 19:37:55
【问题描述】:

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

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();

}

【问题讨论】:

  • catch (Exception e) 捕捉到什么了吗?在那里放置一个输出,不要将catch 块留空
  • 好的,但即使我改变:public void onDestroy() { if (mp !=null) mp.release(); if (isFinishing()) { notificationManager.cancel(NOTIF_ID); } super.onDestroy(); } 它不能解决我的问题

标签: android android-layout android-intent


【解决方案1】:
// try this way
@Override
    public void onDestroy() {
        if(isFinishing()){
            notificationManager.cancelAll();
        }
        super.onDestroy();
}  

【讨论】:

  • NotificationManager.cancel(NOTIF_ID);
【解决方案2】:

尝试调用“killProcess”方法

@Override
public void onDestroy() {
    super.onDestroy();
    android.os.Process.killProcess(android.os.Process.myPid());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-13
    • 2020-07-05
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多