Android中的结束调用
我想在几秒钟后结束一个新的拨出电话。这是我的密码:Android中的结束调用,android,android-intent,telephony,Android,Android Intent,Telephony,我想在几秒钟后结束一个新的拨出电话。这是我的密码: public class phonecalls extends Activity { ComponentName cn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS
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新手,似乎无法让它工作 你不能在Android中结束通话。在调用startActivity(callIntent)时,您将控制权交给本机调用者应用程序。现在只能通过电话应用程序中止通话
此外,启动调用后的代码没有多大意义。您没有对正在创建的ComponentName执行任何操作,因此永远不会调用接收方的onReceive方法。此外,如果将调用接收器,onReceive方法中的代码不会改变电话呼叫的状态。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>
. 有很多人试图这样做(据我所知,没有成功),社区里有一句响亮的“不要这样做!”。您可以启用飞机模式(需要正确设置)并在不久后禁用它
当然,这意味着3G连接会在短时间内(几秒钟)中断。我的解决方案
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");
如果您试图在setResultData()
(即在调用中)之后结束调用。