从钩子方法获取设备传感器状态

时间:2018-05-30 08:37:21

标签: android hook

在我的应用程序中,我试图根据收到的不同设备硬件部件的信息(例如,电池电量或电池正在充电,用户位置等)来改变某些类的功能。 我正在尝试这样做,但在创建NullPointerException时收到IntentFilter。 我的代码是(试图读取电池状态):

Context context = (Context) AndroidAppHelper.currentApplication();
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || 
        status == BatteryManager.BATTERY_STATUS_FULL;

有什么办法吗?

2 个答案:

答案 0 :(得分:0)

请尝试使用以下代码获取电池状态

public class Main extends Activity {
  private TextView batteryTxt;
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
      batteryTxt.setText(String.valueOf(level) + "%");
    }
  };

  @Override
  public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.main);
    batteryTxt = (TextView) this.findViewById(R.id.batteryTxt);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}

答案 1 :(得分:0)

尝试参考以下代码。希望它对你有所帮助。

这是a link

 findAndHookMethod("com.android.systemui.statusbar.policy.BatteryController", lpParam.classLoader, "onReceive", Context.class, Intent.class, new XC_MethodHook() { 
        @Override 
        protected void afterHookedMethod(MethodHookParam param) throws Throwable { 
         try { 
          Intent pi = (Intent) param.args[1]; 
          int status = pi.getIntExtra("status", 1); 
          int blevel = pi.getIntExtra("level", 0); 
          @SuppressWarnings("unchecked") 
          int j = ((ArrayList<ImageView>) getObjectField(param.thisObject, "mIconViews")).size(); 
          for (int k = 0; k < j; k++) { 
           @SuppressWarnings("unchecked") 
           ImageView iv = ((ArrayList<ImageView>) getObjectField(param.thisObject, "mIconViews")).get(k); 
           if (status == 2 && blevel < 100) { 
            AnimationDrawable animation = new AnimationDrawable(); 
            for (int i = 0; i < filearray.size(); i++) { 
             String cbimg = "battery/charge/" + filearray.get(i); 
    //         Log.d("XSBM", "Req charge img: " + cbimg); 
             final Bitmap cb = ZipStuff.getBitmap(path, cbimg, 240); 
             Drawable cd = new BitmapDrawable(null, cb); 
             animation.addFrame(cd, paramPrefs.getInt("charge_delay", 350)); 
            } 
            if (paramPrefs.getBoolean("altcharge", false)) { 
             String bimg = "battery/" + battarray[blevel]; 
             final Bitmap b = ZipStuff.getBitmap(path, bimg, 240); 
             Drawable bd = new BitmapDrawable(null, b); 
             animation.addFrame(bd, 500); 
            }  
            animation.setOneShot(false); 
            iv.setImageDrawable(animation); 
            animation.start(); 
           } else if (status == 2 && blevel >= 100) { 
            String bimg = "battery/" + battarray[100]; 
            final Bitmap b = ZipStuff.getBitmap(path, bimg, 240); 
            Drawable d = new BitmapDrawable(null, b); 
            iv.setImageDrawable(d); 
           } else {  
            String bimg = "battery/" + battarray[blevel]; 
            final Bitmap b = ZipStuff.getBitmap(path, bimg, 240); 
            Drawable d = new BitmapDrawable(null, b); 
            iv.setImageDrawable(d); 
           } 
          } 
         } catch (Throwable t) { XposedBridge.log(t); } 
        } 

       });  
      }