BatteryService学习摘要

来源:互联网 发布:自考跟网络教育的区别 编辑:程序博客网 时间:2024/05/21 17:28

BatteryService学习摘要  

1)JNI(Java Native Interface)
    中文为JAVA本地调用。从Java1.1开始,Java Native Interface(JNI)标准成为java平台的一部分,它允许Java代码和其他语言写的代码进行交互。
2)UEventObserver的使用
    Android很多事件都是通过uevent跟kernel来异步通信的。其中类UEventObserver是核心。
    UEventObserver接收kernel的uevent信息的抽象类。 
    类UEventObserver提供了三个接口给子类来调用:
    (1) onUEvent(UEvent event)
       子类必须重写这个onUEvent来处理uevent。
    (2) startObserving(String match)
       启动进程,要提供一个字符串参数。
    (3) stopObserving()
       停止进程。
    //在BatteryService.java中
     mUEventObserver.startObserving("SUBSYSTEM=power_supply");
     private UEventObserver mUEventObserver = new UEventObserver() {
        @Override
        public void onUEvent(UEventObserver.UEvent event) {
            update();
        }
     };
     在UEvent thread中会不停调用 update()方法,来更新电池的信息数据。
3)关机
        // shut down gracefully if our battery is critically low and we are not powered.
        // wait until the system has booted before attempting to display the shutdown dialog.
        if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
            Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
            intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//if a task is already running for the activity
    // you are now starting, then a new activity will not be started; instead,
    // the current task will simply be brought to the front of the screen with
    // the state it was last in. 
//This flag can not be used when the caller is requesting a result from the activity being launched.
            mContext.startActivity(intent);
        }
4)SystemClock.elapsedRealtime()
Returns milliseconds since boot, including time spent in sleep.
5)EventLog.writeEvent()
EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,mBatteryLevel, mBatteryVoltage, mBatteryTemperature);
6)Intent Flag
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY //If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.
                | Intent.FLAG_RECEIVER_REPLACE_PENDING);
ActivityManagerNative.broadcastStickyIntent(intent, null);
7)logBatteryStats()方法
        IBinder batteryInfoService = ServiceManager.getService(BATTERY_STATS_SERVICE_NAME);
        if (batteryInfoService == null) return;

        DropBoxManager db = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
        if (db == null || !db.isTagEnabled("BATTERY_DISCHARGE_INFO")) return;

        File dumpFile = null;
        FileOutputStream dumpStream = null;
        try {
            // dump the service to a file
            dumpFile = new File(DUMPSYS_DATA_PATH + BATTERY_STATS_SERVICE_NAME + ".dump");
            dumpStream = new FileOutputStream(dumpFile);
            batteryInfoService.dump(dumpStream.getFD(), DUMPSYS_ARGS);
            FileUtils.sync(dumpStream);

            // add dump file to drop box
            db.addFile("BATTERY_DISCHARGE_INFO", dumpFile, DropBoxManager.IS_TEXT);
        } catch (RemoteException e) {
            Slog.e(TAG, "failed to dump battery service", e);
        } catch (IOException e) {
            Slog.e(TAG, "failed to write dumpsys file", e);
        } finally {
            // make sure we clean up
            if (dumpStream != null) {
                try {
                    dumpStream.close();
                } catch (IOException e) {
                    Slog.e(TAG, "failed to close dumpsys output stream");
                }
            }
            if (dumpFile != null && !dumpFile.delete()) {
                Slog.e(TAG, "failed to delete temporary dumpsys file: "
                        + dumpFile.getAbsolutePath());
            }
        }
8)dump(FileDescriptor fd, PrintWriter pw, String[] args)方法
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                != PackageManager.PERMISSION_GRANTED) {

            pw.println("Permission Denial: can't dump Battery service from from pid="
                    + Binder.getCallingPid()
                    + ", uid=" + Binder.getCallingUid());
            return;
        }

9)接收到 BatteryService 发送出来的电池信息
需要注册一个 Intent 为Intent.ACTION_BATTERY_CHANGED 的 BroadcastReceiver 注册方法如下:
 IntentFilter mIntentFilter = new IntentFilter();
 mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
 registerReceiver(mIntentReciver,mIntentFilter); 
 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver(){ 
   @Override
   public void onReceive(Context context, Intent intent){ 
      //TODO Auto-generated method stub String action = intent.getAction();
      If(action.equals(Intent.ACTION_BATTERY_CHANGED)){
         int nVoltaget = intent.getIntExtra(“voltage”,0);
         if(nVoltage!=0){ mVoltage.setText(“v:” + nVoltage + “mV-Success…”);}
         else{
            mVoltage.setText(“V: “+nVoltage+”mV-fall…”);
         }
      }
   }
}
(10) stat_sys_battery_charge.xml
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="14">
        <animation-list
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false">
            <item android:drawable="@drawable/stat_sys_battery_charge_anim0" android:duration="2000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim1" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim2" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim3" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim4" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim5" android:duration="1000" />
        </animation-list>
    </item>
    <item android:maxLevel="29">
        <animation-list
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false">
            <item android:drawable="@drawable/stat_sys_battery_charge_anim1" android:duration="2000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim2" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim3" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim4" android:duration="1000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim5" android:duration="1000" />
        </animation-list>
    </item>
    ...
    <item android:maxLevel="89">
        <animation-list
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:oneshot="false">
            <item android:drawable="@drawable/stat_sys_battery_charge_anim4" android:duration="2000" />
            <item android:drawable="@drawable/stat_sys_battery_charge_anim5" android:duration="1000" />
        </animation-list>
    </item>
    <item android:maxLevel="101" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</level-list>
========================
参考资料
JNI  http://baike.baidu.com/view/1272329.html?wtp=tt
Uevent http://www.cnblogs.com/qclzdh/archive/2011/06/13/2080166.html
有关Linux的.a .so 和.o文件 http://apps.hi.baidu.com/share/detail/23484800
原创粉丝点击