Last active
September 10, 2015 06:16
-
-
Save goodev/d33662ae8ab1de657c5b to your computer and use it in GitHub Desktop.
设置系统字体 fongScale 大小
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 详细信息参考: http://blog.chengyunfeng.com/?p=605 | |
// 在 Manifest.xml 中添加权限 | |
//<uses-permission android:name="android.permission.WRITE_SETTINGS" /> | |
//<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> | |
private Configuration mConfiguration = new Configuration(); | |
/** 获取当前系统的配置信息 */ | |
private void updateConfiguration() { | |
try { | |
Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative"); | |
try { | |
Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative); | |
Object config = am.getClass().getMethod("getConfiguration").invoke(am); | |
Configuration configs = (Configuration) config; | |
mConfiguration.updateFrom(configs); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (SecurityException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** 授权应用的权限*/ | |
private void grantPermision() { | |
try { | |
String cmd = "pm grant " + getActivity().getPackageName() + " android.permission.CHANGE_CONFIGURATION"; | |
CommandCapture command = new CommandCapture(0, false, cmd); | |
RootTools.getShell(true).add(command); | |
commandWait(Shell.startRootShell(), command); | |
boolean result = command.getExitCode() == 0; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (TimeoutException e) { | |
e.printStackTrace(); | |
} catch (RootDeniedException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void commandWait(Shell shell, Command cmd) throws Exception { | |
while (!cmd.isFinished()) { | |
RootTools.log(TAG, shell.getCommandQueuePositionString(cmd)); | |
synchronized (cmd) { | |
try { | |
if (!cmd.isFinished()) { | |
cmd.wait(2000); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (!cmd.isExecuting() && !cmd.isFinished()) { | |
if (!shell.isExecuting && !shell.isReading) { | |
Log.e(TAG, "Waiting for a command to be executed in a shell that is not executing and not reading! \n\n Command: " + cmd.getCommand()); | |
Exception e = new Exception(); | |
e.setStackTrace(Thread.currentThread().getStackTrace()); | |
e.printStackTrace(); | |
} else if (shell.isExecuting && !shell.isReading) { | |
Log.e(TAG, "Waiting for a command to be executed in a shell that is executing but not reading! \n\n Command: " + cmd.getCommand()); | |
Exception e = new Exception(); | |
e.setStackTrace(Thread.currentThread().getStackTrace()); | |
e.printStackTrace(); | |
} else { | |
Log.e(TAG, "Waiting for a command to be executed in a shell that is not reading! \n\n Command: " + cmd.getCommand()); | |
Exception e = new Exception(); | |
e.setStackTrace(Thread.currentThread().getStackTrace()); | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
/** 设置字体*/ | |
public void setFontSize() { | |
Method method; | |
try { | |
Class<?> activityManagerNative = Class.forName("android.app.ActivityManagerNative"); | |
try { | |
Object am = activityManagerNative.getMethod("getDefault").invoke(activityManagerNative); | |
method = am.getClass().getMethod("updatePersistentConfiguration", android.content.res.Configuration.class); | |
method.invoke(am, mConfiguration); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment