是的,这是可能的,但您无法控制Home key
and的行为end call key
。
对于全屏添加android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
到清单文件中的活动标签。
要禁用来电,您需要接听电话:
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() {
}
}
注意:停止服务时不要忘记删除侦听器并将这些权限添加到您的清单文件中:
<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();
}