Android应用:覆盖文件而不是附加

时间:2018-01-29 04:38:23

标签: 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");

    }
}

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

1 个答案:

答案 0 :(得分:3)

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

FileWriter(文件文件,布尔附加)

FileWriter writer = new FileWriter(gpxfile,true);

像这样调用FileWriter构造函数。

您可以详细查看此课程here

相关问题