采用aidl挂断电话

来源:互联网 发布:淘宝无法查询物流信息 编辑:程序博客网 时间:2024/06/11 20:58

aidl 工具类。



我们demo的结构。


布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".EndCallActivity" >    <Button        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="end call" /></RelativeLayout>


activity代码


package org.china.lee.aidlendcalldemo;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import com.android.internal.telephony.ITelephony;import android.app.Activity;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class EndCallActivity extends Activity {private Button btn;private ITelephony iTelephony;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_end_call);try {Method method=Class.forName("android.os.ServiceManager").getMethod("getService",String.class);IBinder bunder=(IBinder) method.invoke(null,new Object[]{TELEPHONY_SERVICE});iTelephony=ITelephony.Stub.asInterface(bunder);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}btn=(Button) super.findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {iTelephony.endCall();} catch (RemoteException e) {e.printStackTrace();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.end_call, menu);return true;}}


0 0