Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148252
  • 博文数量: 41
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-07 08:37
文章分类

全部博文(41)

文章存档

2015年(16)

2013年(9)

2012年(5)

2011年(11)

分类:

2012-11-26 18:55:39


点击(此处)折叠或打开

  1. Init.rc中增加按键显示开关
  2. setprop ro.statusbar.key.support 1

  3. StatusBarServic.java
  4. makeStatusBarView函数会调用创建一个Myticker
  5. mTicker = new MyTicker(context, sb);
  6. MyTicker是继承了Ticker类,并且把Ticker的3个抽样类实例化,增加按键要在里面添加内容
  7. private class MyTicker extends Ticker {
  8.         MyTicker(Context context, StatusBarView sb) {
  9.             super(context, sb);
  10.         }

  11.         @Override
  12.         void tickerStarting() {
  13.             if (SPEW) Slog.d(TAG, "tickerStarting");
  14.             mTicking = true;
  15.             mIcons.setVisibility(View.GONE);
  16.             mHomeBtn.setVisibility(View.GONE);
  17.             mBackBtn.setVisibility(View.GONE);
  18.             mMenuBtn.setVisibility(View.GONE);
  19.             mTickerView.setVisibility(View.VISIBLE);
  20.             mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_up_in, null));
  21.             mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_up_out, null));
  22.             if (mExpandedVisible) {
  23.                 setDateViewVisibility(false, com.android.internal.R.anim.push_up_out);
  24.             }
  25.         }

  26.         @Override
  27.         void tickerDone() {
  28.             if (SPEW) Slog.d(TAG, "tickerDone");
  29.             mTicking = false;
  30.             mIcons.setVisibility(View.VISIBLE);
  31. //wbn

  32.         if (SystemProperties.getInt("ro.statusbar.key.support", 0) != 0) {
  33.             mHomeBtn.setVisibility(View.VISIBLE);
  34.             mBackBtn.setVisibility(View.VISIBLE);
  35.             mMenuBtn.setVisibility(View.VISIBLE);
  36.         }
  37.             mTickerView.setVisibility(View.GONE);
  38.             mIcons.startAnimation(loadAnim(com.android.internal.R.anim.push_down_in, null));
  39.             mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.push_down_out, null));
  40.             if (mExpandedVisible) {
  41.                 setDateViewVisibility(true, com.android.internal.R.anim.push_down_in);
  42.             }
  43.         }

  44.         void tickerHalting() {
  45.             if (SPEW) Slog.d(TAG, "tickerHalting");
  46.             mTicking = false;
  47.             mIcons.setVisibility(View.VISIBLE);
  48. //wbn

  49.         if (SystemProperties.getInt("ro.statusbar.key.support", 0) != 0) {
  50.             mHomeBtn.setVisibility(View.VISIBLE);
  51.             mBackBtn.setVisibility(View.VISIBLE);
  52.             mMenuBtn.setVisibility(View.VISIBLE);
  53.         }
  54.             mTickerView.setVisibility(View.GONE);
  55.             mIcons.startAnimation(loadAnim(com.android.internal.R.anim.fade_in, null));
  56.             mTickerView.startAnimation(loadAnim(com.android.internal.R.anim.fade_out, null));
  57.             if (mExpandedVisible) {
  58.                 setDateViewVisibility(true, com.android.internal.R.anim.fade_in);
  59.             }
  60.         }
  61.     }

  62.     private Animation loadAnim(int id, Animation.AnimationListener listener) {
  63.         Animation anim = AnimationUtils.loadAnimation(StatusBarService.this, id);
  64.         if (listener != null) {
  65.             anim.setAnimationListener(listener);
  66.         }
  67.         return anim;
  68.     }
  69.     
  70.     
  71.     StatusBarPolicy.java
  72.     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver()中增加获得按键控件消息
  73.     else if (action.equals("android.intent.action.ICONKEY_CHANGED")) {
  74.      Log.d(TAG, "Received ACTION_ICONKEY_CHANGED");
  75.      updateIconKeyAction(intent);
  76.                 }
  77.     并且下面增加按键消息增加更新按键的函数
  78.      private void updateIconKeyAction(Intent intent) {
  79.          final int keycode = intent.getIntExtra("keycode", -1);
  80.      Thread sendKeyDelay = new Thread(){
  81.      public void run(){
  82.      IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
  83.      try{
  84.      Thread.sleep(100);
  85.      if (keycode != -1) {
  86.      long now = SystemClock.uptimeMillis();
  87.      KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keycode, 0);
  88.      KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP, keycode, 0);
  89.     
  90.      try {
  91.      wm.injectKeyEvent(down, false);
  92.      }
  93.      catch (RemoteException e) {
  94.      e.printStackTrace();
  95.      Log.i("Input", "DeadObjectException1");
  96.      }
  97.     
  98.      try {
  99.      wm.injectKeyEvent(up, false);
  100.      }
  101.      catch (RemoteException e) {
  102.      e.printStackTrace();
  103.      Log.i("Input", "DeadObjectException2");
  104.      }
  105.      }
  106.      }
  107.             catch (InterruptedException e) {
  108.      e.printStackTrace();
  109.      Log.i("Input", "DeadObjectException3");
  110.      }
  111.          }
  112.          };
  113.          sendKeyDelay.start();
  114.      };
  115.     
  116.     还需要StatusBarPolicy函数中需要添加接收key的Intent消息,这样才能获得消息实例
  117.     filter.addAction("android.intent.action.ICONKEY_CHANGED");
  118.     
  119.     
  120.     
  121.     StatusBarView.java中定义发送这按键消息的函数与显示隐藏函数
  122.      private void sendKeyIntent(int keycode) {
  123.      Intent intent = new Intent("android.intent.action.ICONKEY_CHANGED");
  124.      intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
  125.      intent.putExtra("keycode", keycode);
  126.      sendIntent(intent);
  127.      }
  128.     
  129.      private OnTouchListener homeOnTouch = new OnTouchListener() {
  130.     
  131.      @Override
  132.      public boolean onTouch(View v, MotionEvent event) {
  133.      // TODO Auto-generated method stub

  134.      switch (event.getAction()) {
  135.      case MotionEvent.ACTION_UP:
  136.      {
  137.      sendKeyIntent(KeyEvent.KEYCODE_HOME);
  138.      }
  139.      break;
  140.      }
  141.      return false;
  142.      }
  143.      };
  144.     
  145.      private OnTouchListener backOnTouch = new OnTouchListener() {
  146.     
  147.      @Override
  148.      public boolean onTouch(View v, MotionEvent event) {
  149.      // TODO Auto-generated method stub

  150.      switch (event.getAction()) {
  151.      case MotionEvent.ACTION_UP:
  152.      {
  153.      sendKeyIntent(KeyEvent.KEYCODE_BACK);
  154.      }
  155.      break;
  156.      }
  157.      return false;
  158.      }
  159.      };
  160.     
  161.      private OnTouchListener menuOnTouch = new OnTouchListener() {
  162.     
  163.      @Override
  164.      public boolean onTouch(View v, MotionEvent event) {
  165.      // TODO Auto-generated method stub

  166.      switch (event.getAction()) {
  167.      case MotionEvent.ACTION_UP:
  168.      {
  169.      sendKeyIntent(KeyEvent.KEYCODE_MENU);
  170.      }
  171.      break;
  172.      }
  173.      return false;
  174.      }
  175.      };
  176.     
  177.     public void hiddenHotIcons() {
  178.     //wbn

  179.      if (SystemProperties.getInt("ro.statusbar.key.support", 0) != 0) {
  180.      mHomeBtn.setVisibility(View.INVISIBLE);
  181.      mBackBtn.setVisibility(View.INVISIBLE);
  182.      mMenuBtn.setVisibility(View.INVISIBLE);
  183.      }
  184.      }
  185.     
  186.      public void showHotIcons() {
  187.     //wbn

  188.      if (SystemProperties.getInt("ro.statusbar.key.support", 0) != 0) {
  189.      mHomeBtn.setVisibility(View.VISIBLE);
  190.      mBackBtn.setVisibility(View.VISIBLE);
  191.      mMenuBtn.setVisibility(View.VISIBLE);
  192.      }
  193.      }
  194.     }
  195.     
  196.     
  197.     
  198.     Status_bar.xml布局中增加按键
  199.     <ImageButton android:id="@+id/go_home"
  200.                 android:layout_width="45dip"
  201.                 android:layout_height="wrap_content"
  202.                 android:layout_alignParentRight="true"
  203.                 android:layout_gravity="center_vertical"
  204.                 android:paddingTop="8dip"
  205.                 android:layout_alignParentBottom="true"
  206.                 android:clickable="true"
  207.                 android:background="@drawable/stat_home"
  208.                 />
  209.     
  210.             <ImageButton android:id="@+id/pop_menu"
  211.                 android:layout_width="45dip"
  212.                 android:layout_height="wrap_content"
  213.                 android:layout_alignParentLeft="true"
  214.                 android:layout_gravity="center_vertical"
  215.                 android:paddingTop="8dip"
  216.                 android:layout_alignParentBottom="true"
  217.                 android:clickable="true"
  218.                 android:background="@drawable/stat_menu"
  219.                 />
  220.     
  221.             <ImageButton android:id="@+id/go_back"
  222.                 android:layout_width="45dip"
  223.                 android:layout_height="wrap_content"
  224.                 android:layout_alignParentLeft="true"
  225.                 android:layout_gravity="center_vertical"
  226.                 android:paddingTop="8dip"
  227.                 android:layout_alignParentBottom="true"
  228.                 android:clickable="true"
  229.                 android:background="@drawable/stat_back"
  230.                 />
  231.     
  232.     按键按下与未按下图片显示不一致,所以需要2个图片与图片显示控制文件stat_home.xml,放在drawable目录下,代码如下
  233.     <?xml version="1.0" encoding="utf-8"?>
  234.     <selector
  235.         xmlns:android="">
  236.         <item
  237.             android:state_focused="true"
  238.             android:state_pressed="false"
  239.             android:drawable="@drawable/stat_home_normal" />
  240.     
  241.         <item
  242.             android:state_focused="true"
  243.             android:state_pressed="true"
  244.             android:drawable="@drawable/stat_home_pressed" />
  245.     
  246.         <item
  247.             android:state_focused="false"
  248.             android:state_pressed="true"
  249.             android:drawable="@drawable/stat_home_pressed" />
  250.             
  251.         <item
  252.             android:drawable="@drawable/stat_home_normal" />
  253.     </selector>

阅读(1242) | 评论(0) | 转发(0) |
0

上一篇:GDB调试

下一篇:按键驱动

给主人留下些什么吧!~~