編輯:關於Android編程
效果圖如下:
vc7EvP4gPHN0cm9uZz4uYW5kcm9pZDwvc3Ryb25nPqOsy6u792F2ZM7EvP6jrNXStb3E47XExKPE4sb3tcTOxLz+vNCjrMuru/e08r+qo6zV0rW9PHN0cm9uZz5jb25maWcuaW5pPC9zdHJvbmc+o6y08r+qo6zQ3rjEYXJtaHcuZFBhZD15ZXOjrMb0tq/Eo8TixvejrNXiyrG+zcTc08PBy6GjPC9wPgo8cD48YnI+CjwvcD4KPHA+sb6zzNDyvajBosHLTWFpbkFjdGl2aXR5LmphdmGhok1JRElQbGF5ZXIuamF2YaGiYWN0aXZpdHlfbWFpbi54bWyhozwvcD4KPHA+PGJyPgo8L3A+CjxwPrPM0PK0+sLryOfPwqO6PC9wPgo8cD5NYWluQWN0aXZpdHkuamF2YTxicj4KPC9wPgo8cD48YnI+CjwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">package com.example.l3_sharedpreferences;
import com.example.l3_sharedpreferences.model.MIDIPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView musicStateShow;
private MIDIPlayer midiPlayer;
private boolean musicSate=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicStateShow=(TextView) this.findViewById(R.id.musicStateShow);
midiPlayer=new MIDIPlayer(this);
SharedPreferences sharedPreferences=getSharedPreferences("music",MODE_PRIVATE);
musicSate=sharedPreferences.getBoolean("mState", false);
if(musicSate){
musicStateShow.setText("當前音樂狀態:開");
midiPlayer.playMusic();
}else{
musicStateShow.setText("當前音樂狀態:關");
}
}
/**
* 按鍵彈起事件
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
System.out.println("keyCode="+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
musicStateShow.setText("當前音樂狀態:開");
musicSate=true;
midiPlayer.playMusic();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
musicStateShow.setText("當前音樂狀態:關");
musicSate=false;
midiPlayer.freeMusic();
break;
default:
break;
}
return super.onKeyUp(keyCode, event);
}
/**
* 按鍵按下事件
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//退出應用程序時保存數據
if(keyCode==KeyEvent.KEYCODE_BACK){
SharedPreferences sharedPreferences=getSharedPreferences("music", MODE_PRIVATE);
Editor editor=sharedPreferences.edit(); //取得編輯對象
editor.putBoolean("mState", musicSate); //添加數據
editor.commit(); //保存
if(musicSate){
midiPlayer.freeMusic();
}
finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MIDIPlayer.java
package com.example.l3_sharedpreferences.model; import java.io.IOException; import com.example.l3_sharedpreferences.R; 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(); } } }
一、著色游戲概述近期群裡偶然看到一哥們在群裡聊不規則圖像填充什麼四聯通、八聯通什麼的,就本身好學務實的態度去查閱了相關資料。對於這類著色的資料,最好的就是去搜索些相關ap
Android For JNI(二)——C語言中的數據類型,輸出,輸入函數以及操作內存地址,內存修改器 當我們把Hello World寫完之後,我
榮耀6plus有著紅外遙控功能,相信入手榮耀6plus的同學都很想試試自己的手機當遙控是什麼感覺吧。華為榮耀6plus不僅僅是手機,不僅可以做智能手機,下載
先看看效果圖:package wuwang.tools.utils; import java.io.File; import java.io.FileInputStre