Android数据存储之Files

在默认状态下,文件是不能在不同的程序间共享的。用文件来存储数据可以通过openFileOutput方法打开一个文件(若此文件不存在,则自动创建一个临时文件),通过load方法来获取文件中的数据,通过deleteFile方法来删除一个指定文件

 

Activity:

package com.ko8e;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView view = null;
    private CheckBox checkbox = null;

    private MIDIPlayer PLAYER= null;
    private boolean isplay = false;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        PLAYER = new MIDIPlayer(this);
        view = (TextView) findViewById(R.id.view);
        checkbox = (CheckBox) findViewById(R.id.checkbox);

        /*
         * 文件创建模式:Activity.MODE_APPEND
         * 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
         */ 
        /*
         * 文件创建模式:MODE_PRIVATE
         * 默认模式,在那里创建的文件只能由应用程序调用,即为私有的
         */ 
        /*
         * 文件创建模式:Activity.MODE_WORLD_READABLE
         * 允许所有其他应用程序有读取和创建文件的权限。
         */
         /*
         * 文件创建模式:Activity.MODE_WORLD_WRITEABLE
         * 允许所有其他应用程序具有写入、访问和创建的文件权限。
         */
         
       /* SharedPreferences share = getPreferences(Activity.MODE_PRIVATE);
        isplay = share.getBoolean("isplay", false);
        */
        load();
        if(isplay) {
        	view.setText("当前音乐状态: 开");
        	isplay = true;
        	PLAYER.PlayMusic();
        } else {
        	view.setText("当前音乐状态:关");
        }
        
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked) {
					view.setText("当前音乐状态: 开");
		        	isplay = true;
		        	PLAYER.PlayMusic();
				} else {
					view.setText("当前音乐状态:关");
					isplay = false;
					PLAYER.FreeMusic();
				}
			}
        });
    }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode==KeyEvent.KEYCODE_BACK){
			 /*SharedPreferences uiState=getPreferences(0);
			 SharedPreferences.Editor editor=uiState.edit();
			 editor.putBoolean("isplay", isplay);
			 editor.commit();
			 */
			save();
			 if(isplay)
			 {
			 PLAYER.FreeMusic();
			 }
			 this.finish();
			 return true;
			 }
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return super.onKeyUp(keyCode, event);
	}
	
	public void load() {
		Properties properties = new Properties();
		try {
			FileInputStream fis = this.openFileInput("kobe.cfg");
			properties.load(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		isplay = Boolean.valueOf(properties.get("isplay").toString());
	}
	
	public boolean save() {
		Properties properties = new Properties();
		try {
			//mode-world-writeable允许所有其他运用程序具有写入访问和创建的权限
			FileOutputStream fos = this.openFileOutput("kobe.cfg", Context.MODE_WORLD_WRITEABLE);
			properties.store(fos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
}

 

 

other封装类:

package com.ko8e;

import java.io.IOException;

import android.content.Context;
import android.media.MediaPlayer;

public class MIDIPlayer
{
	public MediaPlayer	playerMusic	= null;
	private Context		mContext	= null;


	public MIDIPlayer(Context context)
	{
		mContext = context;
	}

	/* 播放音乐 */
	public void PlayMusic()
	{
		/* 装载资源中的音乐 */
		playerMusic = MediaPlayer.create(mContext, R.raw.start);
		
		/* 设置是否循环 */
		playerMusic.setLooping(true);
		try
		{
			playerMusic.prepare();
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		playerMusic.start();
	}

	/* 停止并释放音乐 */
	public void FreeMusic()
	{
		if (playerMusic != null)
		{
			playerMusic.stop();
			playerMusic.release();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值