-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
首选项
//先从首选项中把数据读取出来,并显示在textview中
protected void onResume() {super.onResume();
//先从首选项中把数据读取出来,并显示在textview中
SharedPreferences sharedPreferences=this.getSharedPreferences("demo",MODE_PRIVATE );
//首选项可以直接读取数据,但是不能直接写数据。
String time=sharedPreferences.getString("time", "这是您第一次运行本程序");
int degree=sharedPreferences.getInt("degree", 1);
textview.setText(time);
}
//写入当前程序运行的时间。
Editor editor=sharedPreferences.edit();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String str=sdf.format(new Date());
degree++;
editor.putInt("degree", degree);
editor.putString("time", str);
editor.commit();
}
protected void onPause() {
// TODO Auto-generated method stub
SharedPreferences s=getSharedPreferences("demo", MODE_PRIVATE);
String time=s.getString("downtime", "这是您第一次登录");
Editor editor=s.edit();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
newstr=sdf.format(new Date());
editor.putString("downtime", newstr);
editor.commit();
super.onPause();
}
}
2sd卡存储数据
//从sd卡复制一个资源性的文件
所用的是字节流。
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//判断sd卡是否可用
File root=Environment.getExternalStorageDirectory();
File path=new File(root,"image");
File file=new File(path,"image.png");
File file1=new File(path,"images1.png");
try{
FileInputStream input=new FileInputStream(file);
//建立输出流,文件内容立即清空。
//建立输出流,如果文件不存在,会立即生成。
//建立输出流,文件夹不存在,文件夹不会被创建。
FileOutputStream output=new FileOutputStream(file1,true);//不加true
int t;
// while( (t=input.read())!=-1){
// output.write(t);
// }
Input.close();
//关闭流
byte [] buffer=new byte[1024];
while((t=input.read(buffer))!=-1){
output.write(buffer, 0, t);
}
input.close();
output.close();
Toast.makeText(MainActivity.this, "复制完毕", Toast.LENGTH_LONG).show();
}catch(FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(MainActivity.this, "sd卡不可用", Toast.LENGTH_LONG).show();
return;
}
}
});
}
2对话框
Builder builder=new Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("你确定要清零吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
edit.putFloat("breakfast", 0.0f);
edit.putFloat("lunch", 0.0f);
edit.putFloat("dinner", 0.0f);
edit.commit();
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
1, 外部存储,字符流
//如果外部存储状态不可用,直接退出。
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡不可用",Toast.LENGTH_SHORT).show();
return;
}
//创建新文件的方法。
public void createFile() {
File root = Environment.getExternalStorageDirectory();
//获取外部存储卡的目录。
File path = new File(root, "texts");
if (!path.exists()) {
path.mkdirs();
//Creates the directory named by this file, creating missing parent directories if necessary
//创建一个这个文件的目录,如果必要的话创建他的父目录。
}
file = new File(path, "demo.txt");
if (!file.exists()) {
try {
file.createNewFile();
//如果文件不存在,那么创建一个新文件。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2, 文件的读写
//产生随机数,利用文件存储一个最大值。
try {
BufferedReader br = new BufferedReader(new FileReader(file));
//缓冲流,有一个readline()方法,每次可以读一行。
String t;
String temp = "";
while ((t = br.readLine())!= null) {
temp += t+”/n”;
}
br.close();
int max = 0;
if (!temp.equals("")) {
max = Integer.parseInt(temp);
}
if(now >max){
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(now+"");
bw.flush();
bw.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3, sd卡目录的读取、
//这是一个方法,加载list中的数据。
public void loadData(File parent){
list.clear();
File[] f=parent.listFiles();
//这
for(int i=0;i<f.length;i++){
if(!f[i].isHidden()){
list.add(f[i]);
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView) findViewById(R.id.textview);
listview=(ListView) findViewById(R.id.listview);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
root=Environment.getExternalStorageDirectory();
adapter=new MyAdapter();
loadrData(root);
listview.setAdapter(adapter);
}
else{
Toast.makeText(MainActivity.this, "sd卡不可用", Toast.LENGTH_LONG).show();
return ;
}
//
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
if(list.get(arg2).isDirectory()){
now=list.get(arg2);
loadrData(list.get(arg2));
adapter.notifyDataSetChanged();
}
}
});
}
textview.setText(parent.getAbsolutePath());
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
File p=now.getParentFile();
adapter.notifyDataSetChanged();
if(now.getAbsoluteFile().equals(root.getAbsoluteFile())){
super.onBackPressed();
}else{
loadrData(p);
now=p;
}
//super.onBackPressed();
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater l=LayoutInflater.from(MainActivity.this);
View view=l.inflate(R.layout.item, null);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
final ListView sublist = (ListView) view.findViewById(R.id.sublist);
tv_content.setText(list.get(arg0).getName());
ImageView image=(ImageView) view.findViewById(R.id.imageview);
if(list.get(arg0).isDirectory()){
image.setBackgroundResource(R.drawable.icon);
}else{
image.setBackgroundResource(R.drawable.ic_launcher);
}
return view;
}
}