好久没写了,看网上其他类似文章有点麻烦,我的方法较为简单,程序部分只改StatusBarService.java部分就可以了。
在StatusBarService.java
添加
import android.os.ServiceManager; import android.view.IWindowManager; import android.widget.ImageButton;
添加定义控件:
ImageButton homeButton;//对应 "@+id/Stat_Home_button" ImageButton volUpButton;//对应"@+id/Stat_vol_raise_button" ImageButton volDownButton;//对应 "@+id/Stat_vol_down_button" IWindowManager windowManager;
修改makeStatusBarView函数
private void makeStatusBarView(Context context)
为
private void makeStatusBarView(final Context context)
(至于为什么要加这个,不是很懂java,编译器提示要加就加上了-_-|||)
makeStatusBarView函数内部添加每个按钮的Listener来监听onClick
windowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); homeButton = (ImageButton)sb.findViewById(R.id.Stat_Home_button); homeButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ doKey(KeyEvent.KEYCODE_HOME); } }); volUpButton = (ImageButton)sb.findViewById(R.id.Stat_vol_raise_button); volUpButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ doKey(KeyEvent.KEYCODE_VOLUME_UP); } }); volDownButton = (ImageButton)sb.findViewById(R.id.Stat_vol_down_button); volDownButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ doKey(KeyEvent.KEYCODE_VOLUME_DOWN); } });
当按钮监听到onClick时就做相应的处理:
运行一个线程来把ACTION_DOWN和ACTION_UP事件injectKeyEvent,
看上去非常的简单,但是java在这里肯定做了不少事
private void doKey(final int eventCode){ new Thread(new Runnable() { public void run() { long now = SystemClock.uptimeMillis(); try { KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, eventCode, 0); KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, eventCode, 0); windowManager.injectKeyEvent(down, true); windowManager.injectKeyEvent(up, true); } catch (RemoteException e) { Slog.d("Input", "DeadOjbectException"); } } }).start(); }
最后在interceptTouchEvent()函数里面判断一下
....
if (SPEW) { Slog.d(TAG, "Touch: rawY=" + event.getRawY() + " event=" + event + " mDisabled=" + mDisabled); if ((mDisabled & StatusBarManager.DISABLE_EXPAND) != 0) { return false;}} int x1 = (int)event.getRawX(); int Button_l = homeButton.getLeft(); int Button_r = homeButton.getRight(); if(x1>Button_l && x1< Button_r) { Slog.d("pressed homeButton and intercepted"); return false; } Button_l = volUpButton.getLeft(); Button_r = volUpButton.getRight(); if(x1>Button_l && x1< Button_r) { Slog.d( "pressed volUpButton and intercepted"); return false; } Button_l = volDownButton.getLeft(); Button_r = volDownButton.getRight(); if(x1>Button_l && x1< Button_r) { Slog.d("pressed volDownButton and intercepted"); return false; } final int statusBarSize = mStatusBarView.getHeight(); final int hitSize = statusBarSize*2;
....
目的是消费掉对button的点击。
OK,就这样了