我们在手机上安装了一个程序,并将手机借给用户一段时间。我们希望手机仅用于运行我们的应用程序(没有电话,没有游戏,没有任何东西)。手机将被 Root 。
所以我们需要的东西:
- 全屏运行,没有其他内容可见
- 主页按钮和其他设备按钮不起作用
- 我们的应用会在启动时自动运行
它不必是“防黑客”,但应该足以防止普通用户弄乱设备。
这可能吗?我在 Symbian 和 Windows Mobile 上做过类似的事情,但我对 Android 上的这些东西没有太多经验。如何做到这一点?
2015 年更新:如果您不介意将您的应用程序限制为单一手机供应商,三星推出了 KNOX SDK,可让您实现自助服务终端模式,并且无需 root 手机即可轻松实现。详情见:https://seap.samsung.com/developer/sdk/knox-standard-android
最佳答案
可以,但您无法控制 Home key
和 end call key
的行为。
对于全屏添加 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
到 list 文件中的 Activity 标记。
要禁用来电,您需要接听电话:
import android.app.Service;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class MyPhoneStateListener extends Service{
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
class StateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
//Disconnect the call here...
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
@Override
public void onDestroy() {
}
}
注意:停止服务时不要忘记删除监听器并将这些权限添加到 list 文件中:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
并以编程方式断开调用:
try{
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(manager.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephony = (ITelephony)m.invoke(manager);
telephony.endCall();
} catch(Exception e){
Log.d("",e.getMessage());
}
注意:添加此文件以断开调用: http://dl.dropbox.com/u/31740476/ITelephony.aidl
要禁用您需要覆盖的键:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(KeyEvent.KEYCODE_MENU == event.getKeyCode() || KeyEvent.KEYCODE_DPAD_LEFT==event.getKeyCode()
|| KeyEvent.KEYCODE_DPAD_DOWN==event.getKeyCode() || KeyEvent.KEYCODE_DPAD_RIGHT==event.getKeyCode()
|| KeyEvent.KEYCODE_DPAD_UP==event.getKeyCode() || KeyEvent.KEYCODE_DPAD_CENTER==event.getKeyCode()
|| KeyEvent.KEYCODE_BACK==event.getKeyCode())
{
return false;
}
return true;
}
按下 Home 键会出现主屏幕,所以要克服这个问题,您需要实现一个服务,并且您需要实现一个无限线程来重新启动您的应用,如下所示:
public class AppTrackingService extends Service {
private RunnableThread thread;
private Context ctx;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(){
super.onCreate();
ctx = AppTrackingService.this;
thread = new RunnableThread();
}
public void onStart(Intent intent, int startid) {
try{
if(thread==null) thread = new RunnableThread();
thread.startThread();
}catch(Exception e){ }
}
class RunnableThread extends Thread {
Handler back_handler = new Handler();
boolean isContinue = false;
public RunnableThread(){
isContinue = false;
}
public void setIsContinue(boolean val){
this.isContinue = val;
}
public void startThread(){
isContinue = true;
start();
}
public void run(){
ActivityManager actMngr = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
while(isContinue){
try{
//Maintain a boolean "isyourapprunning" to know if your app was running or not....
if(isyourapprunning){
String runningPkg = actMngr.getRunningTasks(1).get(0).topActivity.getPackageName();
if (!runningPkg.equals(ctx.getPackageName())){
launchApp(ctx.getPackageName());
}
Thread.sleep(2500); //2.5 secs
}else{
isContinue = false;
stopSelf();
}
}catch(Exception e){ }
}//end of while loop
}
protected void launchApp(String packageName) {
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
if (null != mIntent) {
try {
startActivity(mIntent);
} catch(Exception e) { }
}
}
}
}
编辑
您需要添加以下权限才能结束通话:
<uses-permission android:name="android.permission.CALL_PHONE" />
您可以使用以下 AIDL 文件:
package com.android.internal.telephony;
/**
* Interface used to interact with the phone. Mostly this is used by the
* TelephonyManager class. A few places are still using this directly.
* Please clean them up if possible and use TelephonyManager instead.
*
* {@hide}
*/
interface ITelephony {
/**
* End call if there is a call in progress, otherwise does nothing.
*
* @return whether it hung up
*/
boolean endCall();
/**
* Silence the ringer if an incoming call is currently ringing.
* (If vibrating, stop the vibrator also.)
*
* It's safe to call this if the ringer has already been silenced, or
* even if there's no incoming call. (If so, this method will do nothing.)
*
* TODO: this should be a oneway call too (see above).
* (Actually *all* the methods here that return void can
* probably be oneway.)
*/
void silenceRinger();
}
关于Android:完全控制手机(kiosk模式),有可能吗?如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7121508/