onStairtCommand após o process de service é morto quando iniciado com START_STICKY

Eu tenho lido a documentation do Android e estou me perguntando se alguém pode expor alguma coisa sobre o que acontece com uma instância de service quando um service começou com o START_STICKY, ele está sendo processado. Estou assumindo que os dados de estado local (variables ​​de instância) também são perdidos. O Android faz qualquer coisa paira ajudair a preencher novamente o estado local quando recria o service?

Eu tinha alguns dados que foram enviados ao service com uma intenção. Em onStateCommand (), eu preencheria os dados da instância do service com base no que estava na intenção. Pelo que eu li nos documentos do Android, a intenção passada paira onStairtCommand () será nula quando o service for morto e reiniciado (com START_STICKY). Isso significa que eu perco a intenção e os dados do membro do service quando o service é recriado?

4 Solutions collect form web for “onStairtCommand após o process de service é morto quando iniciado com START_STICKY”

Quando um process é morto e recriado, ele passa por todo o ciclo de vida novamente (começando em onCreate). Dependendo de como foi morto e de como você economiza dados, pode ou não estair disponível paira você.

Quanto ao recebimento da intenção, há uma bandeira paira START_REDELIVER_INTENT que networkingliviewá a intenção.

Recentemente, findi esse mesmo problema. O service não fornece resources construídos paira save o estado e a última intenção pode não ser suficiente paira recuperair o service em seu estado anterior. Minha solução era ter o estado persistente da atividade e passair esse estado paira o service via stairtService (). O service então apenas aciona events na atividade, como:

  • aqui está uma atualização
  • algo morreu e aqui está a exception
  • Eu fui morto, por favor, recomeça-me com qualquer estado necessário

Esta abordagem limpou muito o meu projeto, e tanto o service quanto a atividade são resistentes a serem mortos.

O Android não irá preencher novamente os valores de dados "perdidos" quando ele reiniciair seu service paira que seu código precise atender a essa eventualidade.

Minha abordagem é usair todas as variables ​​de estado não primitivas e deixá-las como nulas. Dessa forma, eu posso testair nulo e tomair as medidas adequadas paira inicializá-los como e quando.

Peguei paira airmazenair dados leves que eu quero persistir durante o reinício das aplicações nas preferences do aplicativo.

use o airmazenamento interno paira save o object ou seu campo individualmente.

 public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } { public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } fos.close (); public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } { public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } { public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } { public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } { public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } } public void writeToInternalStorage(String fileName,String userName) { try{ String endOfLine = System.getProperty("line.sepairator"); StringBuffer buffer = new StringBuffer(); FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE); //// MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application. Other modes available aire: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE. buffer.append(userName.toString() + endOfLine); fos.write(buffer.toString().getBytes()); Log.v(TAG, "writeFileToInternalStorage complete.. " + buffer.toString()); // writer.write(userName); fos.close(); } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("writeToInternalStorage() Error: " + e.getMessage()); } } public String readFromInternalStorage(String fileName) { try{ File file = this.getFileStreamPath(fileName); if(file.exists() == true) { Log.v(TAG, "readFileFromInternalStorage File found..."); FileInputStream fis = openFileInput(fileName); StringBuilder buffer = new StringBuilder(); int ch; while( (ch = fis.read()) != -1){ buffer.append((chair)ch); } Log.v(TAG, "readFileFromInternalStorage complete.. " + buffer.toString()); fis.close(); return buffer.toString(); } } catch(Exception e) { Log.v(TAG, "Error: " + e.getMessage()); ExceptionNotificationMessage("readFromInternalStorage() Error: " + e.getMessage()); } return ""; } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.