涉及Mp3播放服务
本地数据储存读写
代码简明易懂,适合初学者
使用开发工具 Android Studio 3.2
开发环境:JDK 1.8
废话不多说,有图有真相
public class MusicService extends Service {
private MediaPlayer mMediaPlayer; //定义一个媒体文件
@Override
public void onCreate() {
super.onCreate();
L.e("onCreate");
int RingtoneType = DataAccess.ReadDataInt(this,"RINGTONE");
if (RingtoneType != 0){
switch (RingtoneType){
case 1:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone01_9420); //引入mp3文件
break;
case 2:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone02_matteo_panama); //引入mp3文件
break;
case 3:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone03_planef); //引入mp3文件
break;
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
L.e("onStartCommand");
mMediaPlayer.start(); //启动媒体文件
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
L.e("onDestroy");
mMediaPlayer.stop(); //停止播放
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
L.e("onBind");
return null;
}
}
public class BaseActivity extends AppCompatActivity {
public static Typeface typeFace;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//主题设置
setTheme();
//显示返回键
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
}
//取消任务栏(全屏)
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setTypeface();
}
@Override
public Resources getResources() {
float s = DataAccess.ReadDataFloat(BaseActivity.this,"size");
L.e("---------" + s);
//字体大小缩放
Resources resources = super.getResources();
Configuration configuration = resources.getConfiguration();
if(s == 0.0){
configuration.fontScale = StatcClass.Size;
}else{
configuration.fontScale = s;
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return resources;
}
//字体设置(需要 : - serif
)
public void setTypeface(){
int fontsType = DataAccess.ReadDataInt(BaseActivity.this,"FONTS");
if(fontsType == 0){
typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZHT.TTF");
}else{
switch (fontsType){
case 1: typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZHT.TTF");
break;
case 2: typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZXY.TTF");
break;
case 3: typeFace = Typeface.createFromAsset(getAssets(), "fonts/HWXK.TTF");
break;
case 4: typeFace = Typeface.createFromAsset(getAssets(), "fonts/HWXW.TTF");
break;
}
}
try
{
Field field = Typeface.class.getDeclaredField("SERIF");
field.setAccessible(true);
field.set(null, typeFace);
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
//APP主题设置
public void setTheme(){
int ThemeType = DataAccess.ReadDataInt(BaseActivity.this,"THEME");
switch (ThemeType){
case 0: setTheme(R.style.AppTheme);
break;
case 1: setTheme(R.style.AppTheme01);
break;
case 2: setTheme(R.style.AppTheme02);
break;
case 3: setTheme(R.style.AppTheme03);
break;
case 4: setTheme(R.style.AppTheme04);
break;
}
}
}
public class MainActivity extends BaseActivity implements View.OnClickListener {
private Vibrator mVibrator; //创建震动服务对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String Language = DataAccess.ReadDataString(MainActivity.this,"Language"); //读取保存的语言标识
set(Language); //设置语言
setContentView(R.layout.activity_main);
//获取手机震动的服务
mVibrator=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
initView();
}
@SuppressLint("ClickableViewAccessibility")
private void initView() {
Button Button_Test01 = findViewById(R.id.Button_Test01);
Button Button_Test02 = findViewById(R.id.Button_Test02);
Button Button_Test03 = findViewById(R.id.Button_Test03);
View Setting_RelativeLayout01 = findViewById(R.id.Setting_RelativeLayout01);
View Setting_RelativeLayout02 = findViewById(R.id.Setting_RelativeLayout02);
View Setting_RelativeLayout03 = findViewById(R.id.Setting_RelativeLayout03);
Setting_RelativeLayout01.setOnClickListener(this);
Setting_RelativeLayout02.setOnClickListener(this);
Setting_RelativeLayout03.setOnClickListener(this);
Button Size01 = findViewById(R.id.Size01);
Button Size02 = findViewById(R.id.Size02);
Button Size03 = findViewById(R.id.Size03);
Button Size04 = findViewById(R.id.Size04);
Button Size05 = findViewById(R.id.Size05);
Button Font01 = findViewById(R.id.Font01);
Button Font02 = findViewById(R.id.Font02);
Button Font03 = findViewById(R.id.Font03);
Button Font04 = findViewById(R.id.Font04);
Button Theme01 = findViewById(R.id.Theme01);
Button Theme02 = findViewById(R.id.Theme02);
Button Theme03 = findViewById(R.id.Theme03);
Button Theme04 = findViewById(R.id.Theme04);
Button shake01 = findViewById(R.id.shake01);
Button shake02 = findViewById(R.id.shake02);
Button shake03 = findViewById(R.id.shake03);
Button shake04 = findViewById(R.id.shake04);
Button Ringtone01 = findViewById(R.id.Ringtone01);
Button Ringtone02 = findViewById(R.id.Ringtone02);
Button Ringtone03 = findViewById(R.id.Ringtone03);
Button Ringtone04 = findViewById(R.id.Ringtone04);
Button_Test01.setOnClickListener(this);
Button_Test02.setOnClickListener(this);
Button_Test03.setOnClickListener(this);
Size01.setOnClickListener(this);
Size02.setOnClickListener(this);
Size03.setOnClickListener(this);
Size04.setOnClickListener(this);
Size05.setOnClickListener(this);
Font01.setOnClickListener(this);
Font02.setOnClickListener(this);
Font03.setOnClickListener(this);
Font04.setOnClickListener(this);
Theme01.setOnClickListener(this);
Theme02.setOnClickListener(this);
Theme03.setOnClickListener(this);
Theme04.setOnClickListener(this);
shake01.setOnClickListener(this);
shake02.setOnClickListener(this);
shake03.setOnClickListener(this);
shake04.setOnClickListener(this);
Ringtone01.setOnClickListener(this);
Ringtone02.setOnClickListener(this);
Ringtone03.setOnClickListener(this);
Ringtone04.setOnClickListener(this);
SeekBar seekbar02 = findViewById(R.id.seekBar01);
//int position = readCodingInt("position");
int position = DataAccess.ReadDataInt(MainActivity.this, "position");
seekbar02.setMax(4); //最大值为4
seekbar02.setProgress(position); //默认值为0
seekbar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
L.e("拖动过程中的值:"+ String.valueOf(progress) + ":" + String.valueOf(fromUser));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
L.e("开始滑动时的值:"+ String.valueOf(seekBar.getProgress()));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
L.e("停止滑动时的值:" + String.valueOf(seekBar.getProgress()));
switch (seekBar.getProgress()){
case 0:
StatcClass.Size = 0.5f;
break;
case 1:
StatcClass.Size = 0.7f;
break;
case 2:
StatcClass.Size = 0.9f;
break;
case 3:
StatcClass.Size = 1.1f;
break;
case 4:
StatcClass.Size = 1.3f;
break;
}
DataAccess.DeleteData(MainActivity.this,"size");
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",seekBar.getProgress()); //储存进度条的位置
ResetActivity();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Button_Test01:
set("zh");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","zh"); //更新/储存数据
ResetActivity();
break;
case R.id.Button_Test02:
set("en");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","en"); //更新/储存数据
ResetActivity();
break;
case R.id.Button_Test03:
set("ja");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","ja"); //更新/储存数据
ResetActivity();
break;
case R.id.Setting_RelativeLayout01:
startActivity(new Intent(MainActivity.this,zhanghaoguanli.class));
break;
case R.id.Setting_RelativeLayout02:
startActivity(new Intent(MainActivity.this,yonghufankui.class));
break;
case R.id.Setting_RelativeLayout03:
Toast.makeText(MainActivity.this,"版本号",Toast.LENGTH_SHORT).show();
break;
case R.id.Size01:
StatcClass.Size = 0.5f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",0); //储存进度条的位置
ResetActivity();
break;
case R.id.Size02:
StatcClass.Size = 0.7f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",1); //储存进度条的位置
ResetActivity();
break;
case R.id.Size03:
StatcClass.Size = 0.9f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",2); //储存进度条的位置
ResetActivity();
break;
case R.id.Size04:
StatcClass.Size = 1.1f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",3); //储存进度条的位置
ResetActivity();
break;
case R.id.Size05:
StatcClass.Size = 1.3f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",4); //储存进度条的位置
ResetActivity();
break;
case R.id.Font01:
StatcClass.FONTS = 1;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font02:
StatcClass.FONTS = 2;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font03:
StatcClass.FONTS = 3;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font04:
StatcClass.FONTS = 4;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Theme01:
StatcClass.THEME = 1;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme02:
StatcClass.THEME = 2;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme03:
StatcClass.THEME = 3;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme04:
StatcClass.THEME = 4;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.shake01:
mVibrator.vibrate(new long[]{100,100},-1);
break;
case R.id.shake02:
mVibrator.vibrate(new long[]{100,100,100,1000},0);
break;
case R.id.shake03:
mVibrator.vibrate(new long[]{1,1000},0);
break;
case R.id.shake04:
mVibrator.cancel();
break;
case R.id.Ringtone01:
StatcClass.RINGTONE = 1;
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE); //保存选定的音乐
stopService(new Intent(MainActivity.this,MusicService.class)); //停止服务
startService(new Intent(MainActivity.this,MusicService.class)); //启动服务
break;
case R.id.Ringtone02:
StatcClass.RINGTONE = 2;
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE);
stopService(new Intent(MainActivity.this,MusicService.class));
startService(new Intent(MainActivity.this,MusicService.class));
break;
case R.id.Ringtone03:
StatcClass.RINGTONE = 3;
stopService(new Intent(MainActivity.this,MusicService.class));
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE);
startService(new Intent(MainActivity.this,MusicService.class));
break;
case R.id.Ringtone04:
stopService(new Intent(MainActivity.this,MusicService.class));
break;
}
}
// 本地语言设置 关键代码
private void set(String language_type) {
Locale myLocale = new Locale(language_type);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf,dm);
}
//重启MainActivity,用于设置成功后实时刷新页面
private void ResetActivity(){
Intent intent=new Intent(this, MainActivity.class); //跳转到自己
startActivity(intent);
finish();//关闭自己
overridePendingTransition(0, 0); //去掉Activity切换间的动画
}
}
public class DataAccess {
/** Int **/
//储存数据
public static void SaveDataInt(Context context, String Key, int num){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
//存入数据
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putInt(Key, num);
editor01.apply(); //储存信息
}
//读取数据
public static int ReadDataInt(Context context, String Key){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
int num = sp01.getInt(Key,0);
return num;
}
/** String **/
public static void SaveDataString(Context context, String Key, String data){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putString(Key, data);
editor01.apply();
}
public static String ReadDataString(Context context, String Key){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
String num = sp01.getString(Key,"null");
return num;
}
/** Float **/
public static void SaveDataFloat(Context context, String Key, float data){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putFloat(Key, data);
editor01.apply();
}
public static float ReadDataFloat(Context context, String Key){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
float num = sp01.getFloat(Key,0.0f);
return num;
}
//删除指定Key数据,当 KEY = ALL 时清空所有数据
public static void DeleteData(Context context, String Key){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor = sp01.edit();
if(Key.equals("ALL")){
editor.clear();
}else{
editor.remove(Key);
}
editor.commit();
}
}
用于默认标识
public class StatcClass {
public static float Size = 1.0f; //字体缩放倍数
public static int FONTS = 1; //字体大小
public static int THEME = 0; //主题设置
public static int RINGTONE = 0; //铃声设置
}
扫码下载App
App下载:https://aifabu.com/Q7Fn
源码下载:https://download.csdn.net/download/erp_lxkun_jak/11234212
【注意:使用开发工具是 Android Studio 哦!】
感谢你的查阅,希望可以帮到你,祝你学习愉快!