在应用程序里面实现恢复android的出厂设置。
按照网上一般的做法,却报错:
bCkick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
String pkgName = "com.android.settings";
String className = "com.android.settings.MasterClear";
ComponentName cn = new ComponentName(pkgName, className);
intent.setComponent(cn);
startActivity(intent);
}
});
错误如下:
01-29 14:16:58.942: E/AndroidRuntime(677): FATAL EXCEPTION: main
01-29 14:16:58.942: E/AndroidRuntime(677): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.settings/com.android.settings.MasterClear}; have you declared this activity in your AndroidManifest.xml?
01-29 14:16:58.942: E/AndroidRuntime(677): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.app.Activity.startActivity(Activity.java:3297)
01-29 14:16:58.942: E/AndroidRuntime(677): at com.stb.mylistview.MainActivity$2.onClick(MainActivity.java:46)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.view.View.performClick(View.java:3480)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.view.View$PerformClick.run(View.java:13983)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.os.Handler.handleCallback(Handler.java:605)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.os.Looper.loop(Looper.java:137)
01-29 14:16:58.942: E/AndroidRuntime(677): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-29 14:16:58.942: E/AndroidRuntime(677): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 14:16:58.942: E/AndroidRuntime(677): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 14:16:58.942: E/AndroidRuntime(677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 14:16:58.942: E/AndroidRuntime(677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 14:16:58.942: E/AndroidRuntime(677): at dalvik.system.NativeStart.main(Native Method)
------解决思路----------------------
要想实现恢复出厂设置,首先你的应用要有管理员权限,这个是用户授予你的
你可以看一下API文档
http://www.android-doc.com/guide/topics/admin/device-admin.html
------解决思路----------------------
不会吧,我在三星机子上测的,什么数据都没了的
------解决思路----------------------
貌似是没有加权限
------解决思路----------------------
have you declared this activity in your AndroidManifest.xml? 没加权限吧
------解决思路----------------------
java.lang.SecurityException: Not allowed to start service Intent { act=com.android.internal.os.storage.FORMAT_AND_FACTORY_RESET cmp=android/com.android.internal.os.storage.ExternalStorageFormatter } without permission android.permission.MASTER_CLEAR
提示我需要android.permission.MASTER_CLEAR,但是这是系统级应用的权限,不知道LZ怎么加的。。。
我是这样写的:
Intent intent = new Intent("com.android.internal.os.storage.FORMAT_AND_FACTORY_RESET");
ComponentName COMPONENT_NAME
= new ComponentName("android", "com.android.internal.os.storage.ExternalStorageFormatter");
intent.setComponent(COMPONENT_NAME);
startService(intent);
------解决思路----------------------
1、添加权限: <uses-permission android:name="android.permission.MASTER_CLEAR" />
2、不能直接启动MasterClear.java这个类。系统不许可。靠发送广播的机制吧。sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
祝你好运!
更多内容参见:http://www.drovik.com