第一步
配置AndroidManifest.xml文件
第二步
public class PowerService extends IntentService { public static final String POWER_OFF = "org.foyou.onekeylock.POWER_OFF"; public PowerService() { super("PowerService"); } @Override protected void onHandleIntent(@Nullable Intent intent) { if (intent != null && POWER_OFF.equals(intent.getAction())) { try { Object power = getSystemService(Context.POWER_SERVICE); if (power != null && (power instanceof PowerManager)) { PowerManager powerManager = (PowerManager) power; powerManager.getClass().getMethod("goToSleep", new Class[]{Long.TYPE}).invoke(powerManager, new Object[]{Long.valueOf(SystemClock.uptimeMillis())}); } } catch (Exception e) { System.out.println(e.toString()); } } } }
第三步
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(this, PowerService.class); intent.setAction(PowerService.POWER_OFF); startService(intent); finish(); }
从某Rom中提取出来的代码,我的手机由root,其他手机没试过,我的OK。瘦了一下身,保持大小8KB以内。
发表评论 取消回复