2010-09-29 230 views
3

我试图在几秒钟后结束新的传出呼叫。这里是我的代码:在Android中结束呼叫

public class phonecalls extends Activity { 
    ComponentName cn; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     call(); 
    } 
    private void call() { 
     try { 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:123456789")); 
      startActivity(callIntent); 
      cn = new ComponentName(getApplicationContext(), Receiver.class); 
     } catch (ActivityNotFoundException activityException) { 
      Log.e("dialing-example", "Call failed", activityException); 
     } 
    } 
} 

我的接收器类:

public class Receiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { 
      setResultData(null); 
     } 
    } 
} 

我无法中止呼出。我是Android新手,似乎无法使其工作。

回答

4

您无法在Android中结束通话。目前你正在调用startActivity(callIntent),你放弃了对本地调用者应用程序的控制。现在只能通过电话应用程序中止通话。

除了开始通话后你的代码没有多大意义。你没有对你正在创建的ComponentName做任何事情,所以你的Receiver的onReceive方法将永远不会被调用。另外,如果接收方会被调用,onReceive方法中的代码将不会做任何事情来改变电话的状态。

4

BroadcastReceiver类应拦截所有呼叫。检查接收器类在AndroidManifest.xml正确注册:

<receiver android:name="Receiver" android:exported="true" android:enabled="true"> 
    <intent-filter android:priority="1"> 
     <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
     <action android:name="android.intent.action.PHONE_STATE" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</receiver> 

这么说,我不相信这是可能接收函数完成后取消呼叫。我试着将setResultData(null)移到一个延迟几秒的线程中,但我相信这个没有效果的原因是因为意图已经被执行并且调用已经开始。

这可能是唯一的方法是让您的手脏内部API调用PhoneFactory。有很多尝试做到这一点(据我所知没有成功),并有一个响亮的“不要这样做!”来自社区。

+2

如果你试图结束后拨'setResultData()'(即呼叫),[我已经张贴在这里的答案](HTTP://计算器。 COM /问题/ 599443/Android系统如何对挂起-传出呼叫/ 5314372#5314372)。 – 2011-06-27 21:50:01

1

您可以启用飞行模式(需要适当的设置)并在不久之后将其禁用。

当然,这意味着短时间内(几秒钟)会丢失3G连接。

1

我的解决方案

Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);    
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
getContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");