onStartCommand after service process is killed when started with START_STICKY

后端 未结 4 1722
天命终不由人
天命终不由人 2021-02-13 06:15

I have been reading the Android documentation and I am wondering if anyone can shed some light on what happens to a service instance when a service started with START_STICKY has

4条回答
  •  感动是毒
    2021-02-13 06:46

    use Internal Storage for Saving object or its field individually.

    public void writeToInternalStorage(String fileName,String userName)
        {
            try{
                String endOfLine = System.getProperty("line.separator");
                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 are: 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((char)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 "";
        }
    

提交回复
热议问题