Android SharedPreferences和File
package android.basic.lesson19; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.media.MediaPlayer; import android.os.IBinder; import android.widget.Toast; public class MusicService extends Service { //定义MediaPlayer播放器变量 MediaPlayer mPlayer = new MediaPlayer(); @Override public void onCreate() { Toast.makeText(getApplicationContext(), "Service onCreate()", Toast.LENGTH_LONG).show(); //创建播放器 mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.babayetu); //设置自动循环 mPlayer.setLooping(true); } @Override public IBinder onBind(Intent intent) { Toast.makeText(getApplicationContext(), "Service onBind()", Toast.LENGTH_LONG).show(); //获得SharedPreferences对象 SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE); //播放器跳转到上一次播放的进度 mPlayer.seekTo(preferences.getInt("CurrentPosition", 0)); //开始播放 mPlayer.start(); return null; } @Override public boolean onUnbind(Intent intent){ Toast.makeText(getApplicationContext(), "Service onUnbind()", Toast.LENGTH_LONG).show(); //获得SharedPreferences对象 SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE); Toast.makeText(getApplicationContext(), "CurrentPosition="+mPlayer.getCurrentPosition(), Toast.LENGTH_LONG).show(); //获得editor对象,写入一个整数到SharePreferences中,记住要用commit()提交,否则不会实现写入操作 preferences.edit().putInt("CurrentPosition",mPlayer.getCurrentPosition()).commit(); mPlayer.stop(); return false; } } <?xml version="1.0" encoding="utf-8"?> <manifest android:versionname="1.0" android:versioncode="1" xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson19"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name=".MainHelloSharedPreferences"> <intent -filter=""> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </category></action></intent> </activity> <service android:name=".MusicService" android:enabled="true"> </service> </application> <uses android:minsdkversion="8" -sdk=""> </uses></manifest> <?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:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="SharedPreferences的使用" android:id="@+id/TextView01"> </textview> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="绑定音乐播放服务" android:id="@+id/Button01" android:layout_margintop="10dp"> </button> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="解绑定音乐播放服务" android:id="@+id/Button02" android:layout_margintop="10dp"> </button> </linearlayout> package android.basic.lesson19; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainHelloSharedPreferences extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //定义UI组件 Button b1 = (Button) findViewById(R.id.Button01); Button b2 = (Button) findViewById(R.id.Button02); //定义ServiceConnection对象 final ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { } @Override public void onServiceDisconnected(ComponentName name) { } }; //定义按钮的单击监听器 OnClickListener ocl = new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainHelloSharedPreferences.this, android.basic.lesson19.MusicService.class); switch (v.getId()) { case R.id.Button01: Toast.makeText(getApplicationContext(), "Button01 onClick", Toast.LENGTH_LONG).show(); //绑定服务 bindService(intent,conn,Context.BIND_AUTO_CREATE); break; case R.id.Button02: Toast.makeText(getApplicationContext(), "Button02 onClick", Toast.LENGTH_LONG).show(); //取消绑定 unbindService(conn);  
1、SharedPreferences
2、Android中的文件IO操作