我正在尝试在我的应用程序中创建一个"注销"功能.基本上,通过注销,应清除应用程序数据.我想要做的是在注销后,应用程序应该重新启动,以便可以再次输入凭据等.我遇到的问题是,当用户单击"注销"时,应用程序已经运行了3-4个活动,我不知道如何退回它们.我如何(模拟?)重新启动应用程序?
我需要编译具有系统权限的应用程序才能使用目标应用程序com.android.settings.现在,当我尝试运行我的apk时,我收到错误消息
测试运行失败:权限拒绝:从pid = 354开始检测ComponentInfo {com.jayway.test/android.test.InstrumentationTestRunner},因为包com.jayway.test没有与目标com匹配的签名,所以不允许使用uid = 354. android.settings
如何使用系统权限编译我的应用程序?
有没有办法直接从应用程序重启/关闭手机?
例如:当我遇到某些特定情况时,我需要重新启动/关闭手机...
引用开发者网站:
重启权限?
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
砖设备的许可???
http://developer.android.com/reference/android/Manifest.permission.html#BRICK
重启的方法???
http://developer.android.com/reference/android/os/PowerManager.html#reboot%28java.lang.String%29
重启和擦除的方法??
MonkeyRunner/MonkeyDevice中的重启方法:
http://developer.android.com/guide/developing/tools/MonkeyDevice.html#reboot
有一些选项可以阻止设备,但为什么不关闭或重启?
我尝试了以下代码,但它抛出异常..
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.schogini.PowerOff"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.DEVICE_POWER" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PowerOffActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
Java代码:
package com.schogini.PowerOff;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;
public class PowerOffActivity extends Activity {
PowerManager pm;
/** Called when the activity is first …Run Code Online (Sandbox Code Playgroud) 在我的Android应用程序中,我想在按钮单击时重新启动我的Android设备.但它不起作用.
到目前为止我已经这样做了.
ImageButton restartmob = (ImageButton) this.findViewById(R.id.restartmob);
restartmob.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
Process proc = null;
try {
//proc = Runtime.getRuntime().exec(new String[] { "su", "-c", "reboot" });
proc = Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});
proc.waitFor();
} catch (Exception ex) {
Log.i(TAG, "Could not reboot", ex);
}
}
});
Run Code Online (Sandbox Code Playgroud)
我还在清单文件中添加了以下权限.
<permission android:name="android.permission.REBOOT"/>
Run Code Online (Sandbox Code Playgroud)
当我单击图像按钮重新启动时,我得到以下异常.
java.io.IOException: Error running exec(). Command: [/system/bin/su, -c, reboot now]
Working Directory: null Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:128)
at com.android.onlinepayment.activity.SystemSubMenuActivity$1.onClick(SystemSubMenuActivity.java:49)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910) …Run Code Online (Sandbox Code Playgroud)