从后台不工作的Android读取和写入外部存储文件

时间:2018-04-10 10:29:33

标签: java android

在我的应用程序中,我正在使用FirebaseMessagingService,我正在接收通知,对于这些通知,我在外部存储中创建日志,当我的应用程序处于运行模式时,日志创建完美,但在应用程序之后已被杀死,日志没有被创建,但通知即将到来。我不明白是什么问题,在logcat中也没有任何异常。

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {

public String message = null;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

message=remoteMessage.getNotification().getBody();


if(message.contains("Text")){
        try {
            intent1 = new Intent(this, ResultActivity.class);
            intent1.putExtra("TextData", message);
            intent1.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT );
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(message);
            notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBuilder.setAutoCancel(true);
            notificationBuilder.setContentIntent(pendingIntent);
            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1, notificationBuilder.build());

            try {

                File file = new File(getExternalFilesDir("MyStorage"),strNotificationFileName);
                if (file.exists())
                {
                    IsFileExist=true;
                    strNotificationFileName=file.getPath();
                }
                else
                {

                    IsFileExist=true;
                    try{
                        file.createNewFile();
                        strNotificationFileName=file.getPath();
                    }catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            catch (Exception ex)
            {
            }

            try {
                ArrayList<Object> listNotificationread=readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            UpdateNotificationList("Text");

        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }

    }
    }
}

public ArrayList<Object> readObject() throws ClassNotFoundException, IOException {
    File file = new File(getExternalFilesDir("MyStorage"),"Notification.xml");
    if (file.exists()) {

    }
    listNotification = new ArrayList();
    //Create new FileInputStream object to read file
    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    //Create new ObjectInputStream object to read object from file
    ObjectInputStream obj = new ObjectInputStream(fis);
    try {
        while (fis.available() != -1) {
            //Read object from file
            NotificationSavedList acc = (NotificationSavedList) obj.readObject();
            listNotification.add(acc);
        }
    } catch (EOFException ex) {
        System.out.println(ex.getMessage());
    }
    //Collections.reverse(listNotification);
    return listNotification;
}


public void writeObject(ArrayList<Object> listNotification) throws IOException {
    File file = new File(getExternalFilesDir("MyStorage"),"Notification.xml");

    //Create FileOutputStream to write file
    FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
    //Create ObjectOutputStream to write object
    ObjectOutputStream objOutputStream = new ObjectOutputStream(fos);
    //Write object to file
    for (Object obj : listNotification) {
        objOutputStream.writeObject(obj);
        objOutputStream.reset();
    }
    objOutputStream.close();



}

public void UpdateNotificationList(String type) {

    try {

        try {
            if (listNotification.size() > 20) {
                listNotification.remove(0);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        Date currentTime = Calendar.getInstance().getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        String parsedDate = formatter.format(currentTime);

        NotificationSavedList _NotificationSavedList = new NotificationSavedList(listNotification.size()+1, type, message, parsedDate);
        listNotification.add(_NotificationSavedList);

        writeObject(listNotification);

    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}

}

0 个答案:

没有答案