如何在android中卸载应用程序之前保存数据?
在Android中,一旦用户从管理应用程序->应用程序名称->清除数据中清除数据,SharedReference和SQLite数据库就会被清除。如果在安装应用程序之前应该保留一些信息,该怎么办。我想存储信息,直到应用程序安装到设备中 有没有办法做到这一点如何在android中卸载应用程序之前保存数据?,android,save,Android,Save,在Android中,一旦用户从管理应用程序->应用程序名称->清除数据中清除数据,SharedReference和SQLite数据库就会被清除。如果在安装应用程序之前应该保留一些信息,该怎么办。我想存储信息,直到应用程序安装到设备中 有没有办法做到这一点 在应用程序的生命周期之前,没有存储数据的方法吗?我已经读到2.2版本中有getExternalFiles()选项可用,但这同样取决于SD卡。应用程序无法在Android设备上永久存储数据吗?使用文件并将其写入内存或外部存储器。使用加密,以防您需
在应用程序的生命周期之前,没有存储数据的方法吗?我已经读到2.2版本中有
getExternalFiles()
选项可用,但这同样取决于SD卡。应用程序无法在Android设备上永久存储数据吗?使用文件并将其写入内存或外部存储器。使用加密,以防您需要它是安全的
编辑:android的某些版本有一个选项,您可以使用该选项删除与特定应用程序相关的所有数据,包括文件。您的应用程序必须就此向用户发出警告,并且您必须引入此功能以处理文件被擦除时的情况您可以使用文件系统存储在内部sd卡中如果您不希望以这种方式清除数据,您可能不应该使用首选项,因为用户可以自愿清除它们 如果再次选择SD卡作为存储介质,则用户可以自行选择从其存储中删除内容 因此,剩下的另一个选项是使用应用程序的内部内存。您可以通过此链接进行此操作
@所有要求使用内部存储的人 如果您转到设置>应用程序>管理应用程序>(应用程序)>清除数据,您正在谈论的文件将被删除。这只是清除应用程序存储的数据的一种方法。此方法将擦除应用程序自首次运行以来存储的所有内容 “永久”存储数据的唯一方法是将数据存储在您可以控制的对象上,例如服务器上的数据库 编辑:这里有一个简单的快速脏的测试用例来存储和显示保存的内部文件。此测试用例是对@sunil comment的响应
public class TestFileActivity extends Activity {
private static final String FILENAME = "hello_file";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byte[] data = null;
String collected = null;
try {
FileInputStream fis = openFileInput( FILENAME );
data = new byte[fis.available()];
while(fis.read(data) != -1) {
collected = new String( data );
}
fis.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button b1 = (Button)findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v ) {
putInFile();
}
});
if( collected != null ) {
TextView tv = (TextView)findViewById( R.id.textView1 );
tv.setText( collected );
}
}
private void putInFile() {
String string = "hello world!";
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
</LinearLayout>
公共类TestFileActivity扩展活动{
私有静态最终字符串FILENAME=“hello\u file”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字节[]数据=null;
收集的字符串=null;
试一试{
FileInputStream fis=openFileInput(文件名);
数据=新字节[fis.available()];
而(fis.读取(数据)!=-1){
收集=新字符串(数据);
}
fis.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
按钮b1=(按钮)findViewById(R.id.button1);
b1.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
putinfle();
}
});
如果(已收集!=null){
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(已收集);
}
}
私有无效putinfle(){
String=“你好,世界!”;
试一试{
FileOutputStream fos=openFileOutput(文件名,Context.MODE\u PRIVATE);
fos.write(string.getBytes());
fos.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
即使使用“清除数据”清除数据,内部文件也不会被删除。
我将在我的下一个项目中使用同样的想法。想法是-->使用XML格式将数据存储在文件中,并使用XML解析器使用相同的格式。我希望它对我有用,并提供足够的提示来使用它。
以下是如何使用内部存储器的提示-->
String filename=“file.txt”;
文件输出流;
fos=openFileOutput(文件名、上下文、模式\追加);
XmlSerializer serializer=Xml.newSerializer();
序列化器。设置输出(fos,“UTF-8”);
serializer.startDocument(null,Boolean.valueOf(true));
serializer.setFeature(“http://xmlpull.org/v1/doc/features.html#indent-输出“,真实);
serializer.startTag(null,“root”);
对于(int j=0;j<3;j++)
{
serializer.startTag(null,“记录”);
serializer.text(数据);
serializer.endTag(null,“记录”);
}
serializer.endDocument();
serializer.flush();
fos.close();
要使用DOM解析器读回数据,请执行以下操作:
FileInputStream fis = null;
InputStreamReader isr = null;
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
char[] inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
/*
* converting the String data to XML format
* so that the DOM parser understand it as an XML input.
*/
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();
XmlData xmlDataObj;
DocumentBuilderFactory dbf;
DocumentBuilder db;
NodeList items = null;
Document dom;
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
dom = db.parse(is);
// normalize the document
dom.getDocumentElement().normalize();
items = dom.getElementsByTagName("record");
ArrayList<String> arr = new ArrayList<String>();
for (int i=0;i<items.getLength();i++){
Node item = items.item(i);
arr.add(item.getNodeValue());
}
FileInputStream fis=null;
InputStreamReader isr=null;
fis=context.openFileInput(文件名);
isr=新的输入流阅读器(fis);
char[]inputBuffer=新字符[fis.available()];
isr.read(输入缓冲区);
数据=新字符串(inputBuffer);
isr.close();
fis.close();
/*
*将字符串数据转换为XML格式
*以便DOM解析器将其理解为XML输入。
*/
InputStream is=新的ByteArrayInputStream(data.getBytes(“UTF-8”);
ArrayList xmlDataList=新的ArrayList();
XmlData xmlDataObj;
DocumentBuilderFactoryDBF;
文档生成器数据库;
节点列表项=null;
文档dom;
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
dom=db.parse(is);
//规范文档
dom.getDocumentElement().normalize();
items=dom.getElementsByTagName(“记录”);
ArrayList arr=新的ArrayList();
for(int i=0;iI)不想使用SD卡存储此信息。是否有特定于应用程序的目录
FileInputStream fis = null;
InputStreamReader isr = null;
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
char[] inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
/*
* converting the String data to XML format
* so that the DOM parser understand it as an XML input.
*/
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();
XmlData xmlDataObj;
DocumentBuilderFactory dbf;
DocumentBuilder db;
NodeList items = null;
Document dom;
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
dom = db.parse(is);
// normalize the document
dom.getDocumentElement().normalize();
items = dom.getElementsByTagName("record");
ArrayList<String> arr = new ArrayList<String>();
for (int i=0;i<items.getLength();i++){
Node item = items.item(i);
arr.add(item.getNodeValue());
}