2015-11-18 18 views
0

我正在開發Android Studio。我有一項服務,可生成在網絡中檢測到更改(WiFi,3G)時更新的通知。該服務由交換機控制,並生成用戶無法刪除的通知。 當我停止服務時,我使用取消(id)來刪除通知。 問題是,當我嘗試再次激活服務時,不會生成通知。有沒有什麼辦法解決這一問題? 這些是我生成通知的代碼行: PD。也不例外產生取消通知後,不能再次通知具有相同ID的通知。爲什麼?

編輯

我試圖創建另一個ID的通知,但它不工作。

protected void onProgressUpdate(String... values) { 

     WifiManager wifimanager = (WifiManager) context.getSystemService(WIFI_SERVICE); 

     //Datos 
     try { 
      if(wifimanager.isWifiEnabled() && values[0].equalsIgnoreCase("false") && values[1].equalsIgnoreCase("true")) { 
       NotificationManager manager; 
       Notification myNotication; 
       manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
       Intent notificacionIntent = new Intent(context.getApplicationContext(), Servicio.class); 
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, notificacionIntent, 0); 
       Notification.Builder builder = new Notification.Builder(context); 
       builder.setTicker("Wifi-3G"); 
       builder.setContentTitle("Wifi-3G Notification"); 
       builder.setContentText(getString(R.string.conectado_wifi)); 
       builder.setSmallIcon(R.mipmap.logo_agus_android); 
       builder.setContentIntent(pendingIntent); 
       builder.setOngoing(true); 
       //builder.setAutoCancel(true); 
       myNotication = builder.build(); 
       manager.notify(11, myNotication); 
      } 

在這裏,我取消服務:

swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      DatabaseHandler db = new DatabaseHandler(Servicio.this); 
      auxS = Boolean.toString(isChecked); 
      Preferencia pref = new Preferencia(2,"Servicio", auxS); 
      db.updatePrefe(pref); 
      if (isChecked){ 
       startService(new Intent(Servicio.this, MyService.class)); 
       Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class"); 
       db.addNotificacion(noti); 
      } 
      else { 
       stopService(new Intent(Servicio.this, MyService.class)); 
       Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class"); 
       db.deleteNotificacion(noti); 
      } 
     } 
    }); 

我取消的onDestroy通知()方法:

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Toast.makeText(this, R.string.servicio_destruido, Toast.LENGTH_SHORT).show(); 
    DatabaseHandler db = new DatabaseHandler(this); 
    pref=new Preferencia(3,"EstadoServ","false"); 
    db.updatePrefe(pref); 
    NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
    manager.cancel(11); 
    Conexiones.cancel(true); 
} 

我不是講英語,這是我的第一個問題, 對於那個很抱歉。如果您需要更多關於我的代碼的信息,請不要猶豫,問我。

+0

嘗試在執行代碼後調用super.onDestroy()。 @Victor – lyc001

回答

0

解決,問題在於onCancelled()方法沒有被使用。

-1

您可以更新通知,但只要內存中的應用程序不能使用相同的通知ID。

+0

出於某種原因,所有通知都被阻止,包括具有不同ID的通知。 – Victor

+0

如果用於PlayStore的用戶標識沒有允許來自應用的通知,則會發生這種情況。使用OAuth請求用戶允許通知。 – TheAndroidFreak