java - 安卓应用程序 :Overwriting file instead of appending

标签 java android

我正在尝试将手机中收到的通知附加到文本文件中。但不是附加其覆盖,即仅将最后一个通知存储在文本文件中。我还在应用程序屏幕上显示通知以验证这一点。

我的 MainActivity.java 是:

public class MainActivity extends Activity {
    TableLayout tab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tab = (TableLayout)findViewById(R.id.tab);
        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg"));
    }
    private BroadcastReceiver onNotice= new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

                String pack = intent.getStringExtra("package");
                String title = intent.getStringExtra("title");
                String text = intent.getStringExtra("text");
                 com.example.vk9621.notification1.Filehelper.generateNoteOnSD(getApplicationContext(),"Notification",pack,title,text);
                TableRow tr = new TableRow(getApplicationContext());
                tr.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                TextView textview = new TextView(getApplicationContext());
                textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT,1.0f));
                textview.setTextSize(20);
                textview.setTextColor(Color.parseColor("#0B0719"));
                textview.setText(Html.fromHtml(pack +"<br><b>" + title + " : </b>" + text));
                tr.addView(textview);
                tab.addView(tr);

        }
    };
}

我的 FileHelper.java 是:

public class Filehelper {

    public static void generateNoteOnSD(Context context, String sFileName, String sBody1,String sBody2, String sBody3) {
        try {
            File gpxfile;
            File root = new File(Environment.getExternalStorageDirectory(), "Notes");
            if (!root.exists()) {
                root.mkdirs();
            }
            File file=new File(sFileName);
            if(!file.exists()) {
                gpxfile = new File(root, sFileName);
            }
            else{
                    gpxfile = file;
                }
            FileWriter writer = new FileWriter(gpxfile);
            writer.append(sBody1);
            writer.append(sBody2);
            writer.append(sBody3);
            writer.flush();
            writer.close();
            Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的NotificationService.java是:

public class NotificationService extends NotificationListenerService {
    Context context;

    @Override

    public void onCreate() {

        super.onCreate();
        context = getApplicationContext();

    }
    @Override

    public void onNotificationPosted(StatusBarNotification sbn) {


        String pack = sbn.getPackageName();
        String ticker = sbn.getNotification().tickerText.toString();
        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();

        Log.i("Package",pack);
        Log.i("Ticker",ticker);
        Log.i("Title",title);
        Log.i("Text",text);

        Intent msgrcv = new Intent("Msg");
        msgrcv.putExtra("package", pack);
        msgrcv.putExtra("ticker", ticker);
        msgrcv.putExtra("title", title);
        msgrcv.putExtra("text", text);

        LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);


    }

    @Override

    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i("Msg","Notification Removed");

    }
}

有人可以告诉我错误是什么以及为什么所有通知都没有出现在文本文件中吗?

最佳答案

可能您需要在 FileWriter 类中提供附加参数,如下所示,

FileWriter(文件文件, boolean 追加)

FileWriter writer = new FileWriter(gpxfile,true);

像这样调用 FileWriter 构造函数。

您可以详细查看该类 here

关于java - 安卓应用程序 :Overwriting file instead of appending,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494490/

相关文章:

java - Java 中的线程安全访问枚举

java - 字节与字符串之间的奇怪转换

android - 如何随时停止运行线程并随时启动

android - 如何在模拟器 Android 上测试具有不同版本代码和小数据库更改的 apk

java - Android 翻译的 Canvas 矩形碰撞

java - 空指针异常 : An error occurred while executing doInBackground() :application crash

java - Gurobi 模型最优但违反约束

java - 如何抛出多个异常的不同方式

java - 如何验证 JPA 实体的网站 URL 字段?

android - TabLayout 标题未正确显示