gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:59:22 26 4
gpt4 key购买 nike

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

我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com