2015年5月24日 星期日

偵測耳機硬體鍵單擊雙擊判斷

以上一篇為例,還需準備一個共用class,在共用的class裡設置一個int count = 0;

當點擊硬體鍵時,共用class裡的count++,再搭配cTimer.schedule(cTimerTask,500);

執行後500毫秒才開始判斷count ==1 或==2 在傳遞訊息給主程式執行播放、暫停、下一首。



首先要在MainFest添加共用class:

<application
        android:name=".PP"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:launchMode="singleInstance" >
<activity
.
.


共用class:

public class PP extends Application {

int count =0;
private MyHandler handler ;


    // 傳遞訊息的方法  
    public void setHandler(MyHandler handler) {
        this.handler = handler;
    }
     
    // 得到訊息方法  
    public MyHandler getHandler() {
        return handler;
    }
   
    //點擊硬體鍵count++
    public void addcount(){
    count++;
    }
   
    //得到硬體鍵點擊的次數
    public int getcount(){
    return count;
    }
   
    //回復count初始化
    public void recovercount(){
    count = 0;
    }
}



BroadcastReceiver.java

public class MediaButtonReceiver extends BroadcastReceiver {
private static final String TAG = "MediaButtonReceiver";
private PP pp ; //共用class
private static final int CHANGED = 0x0010;
private static final int CHANGEDNEXTSONG = 0x0011;
private MyHandler mHandler ;  
private boolean isStart = true;
private Timer cTimer = null;


@Override
public void onReceive(final Context context, Intent intent) {

// 獲得Action
String intentAction = intent.getAction();
// 獲得KeyEvent对象
KeyEvent keyEvent = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
// 按下 / 鬆開按鈕
int keyAction = keyEvent.getAction();
pp = (PP)context.getApplicationContext();
mHandler = pp.getHandler();  

if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction) && (KeyEvent.ACTION_DOWN == keyAction)) {

// 獲得按鍵字節碼
int keyCode = keyEvent.getKeyCode();

if (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE == keyCode) {

}
if (KeyEvent.KEYCODE_HEADSETHOOK == keyCode) {
pp.addcount();
if (cTimer == null){
cTimer = new Timer();
cTimer.schedule(cTimerTask,500);
}
cTimer=null;

}
if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == keyCode) {
}
if (KeyEvent.KEYCODE_MEDIA_STOP == keyCode) {
}
// 輸出點擊的按鍵碼
} else if (KeyEvent.ACTION_UP == keyAction) {

}

}

private TimerTask cTimerTask = new TimerTask() {
@Override
public void run() {
isStart = true;

if (isStart) {
if (pp.getcount() == 1 ){

// 發送消息,判斷當點1下,發送CHANGED到class,再由Activity接收
mHandler.sendEmptyMessage(CHANGED); 

} else if (pp.getcount() == 2 || pp.getcount() > 2){

                 // 發送消息,判斷當點2下,發送CHANGEDNEXTSONG到class,再由 Activity接收
mHandler.sendEmptyMessage(CHANGEDNEXTSONG);

}
pp.recovercount();//初始化共用count=0;

}
isStart = false;
}
};

}



Activity.java

public class PlayListActivity extends Activity {

private static final int CHANGED = 0x0010;
private static final int CHANGEDNEXTSONG = 0x0011;
        private MyHandler handler ; 

.
.


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
                setContentView(R.layout.playlist);
listView = (ListView) findViewById(R.id.listView);
imgFront = (ImageView) findViewById(R.id.front);
imgPlay = (ImageView) findViewById(R.id.play);
imgPause = (ImageView) findViewById(R.id.pause);
.
.
.


                pp = (PP) getApplicationContext();
handler = new MyHandler();{
pp.setHandler(handler); 


}

.
.
.



public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == CHANGED) {

  // 當接收的訊息為CHANGED,也就是點一下,播放歌曲
           
}
            }  else if (msg.what == CHANGEDNEXTSONG){

 // 當接收的訊息為CHANGEDNEXTSONG,也就是點兩下,下一首
         
            }
        }
}