AndroidCall

API文档
http://www.android-doc.com/guide/components/index.html

package com.test.testcall;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private TextView textContent;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
    }

    void InitView(){
         textContent = (TextView)findViewById(R.id.textView2);
         btn = (Button)findViewById(R.id.button1);

         btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                textContent.setText("18591939576");
                String tempStr = textContent.getText().toString();
                if(tempStr.length() != 0){
                    Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + tempStr));
                    startActivity(phoneIntent);
                }else{
                    Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_LONG).show();
                }
            }
         });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
package com.test.testcall;

import java.lang.reflect.Method;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.ITelephony;
import android.content.Context;

public class MainActivity extends ActionBarActivity {

    private   ITelephony iPhoney=null;
    private TelephonyManager manager;

    private TextView textContent;
    private Button btn;

    private boolean runnable=true;
    private boolean endCalls=false;
    String tempStr;
    Thread t=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
    }

    void InitView(){
         textContent = (TextView)findViewById(R.id.textView2);
         btn = (Button)findViewById(R.id.button1);
         final TelephonyManager tm=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
         iPhoney=getITelephony(this);//获取电话实例



         btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                textContent.setText("18591939576");
                tempStr = textContent.getText().toString();
                if(tempStr.length() != 0){
//                  Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + tempStr));
//                  startActivity(phoneIntent);
                    t.start();
                }else{
                    Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_LONG).show();
                }
            }
         });

         t=new Thread(new Runnable() {

               @Override
               public void run() {
               try {
                  while(runnable){

                Thread.sleep(5000);//延时5s
                int state=tm.getCallState();
                if(state==TelephonyManager.CALL_STATE_IDLE){
                 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tempStr));
                 startActivity(intent);
                }
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){
                 Thread.sleep(10000);
                 endCalls= iPhoney.endCall();
                 //System.out.println("是否成功挂断:"+endCall);
                }


                  }
               } catch (Exception e)
                  {
                e.printStackTrace();
                  }
               }
              });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 通过反射得到实例
     * @param context
     * @return
     */
    private static ITelephony getITelephony(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager) context
                .getSystemService(TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method getITelephonyMethod = null;
        try {
            getITelephonyMethod = c.getDeclaredMethod("getITelephony",
                    (Class[]) null); // 获取声明的方法
            getITelephonyMethod.setAccessible(true);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        ITelephony iTelephony=null;
        try {
             iTelephony = (ITelephony) getITelephonyMethod.invoke(
                    mTelephonyManager, (Object[]) null); // 获取实例
            return iTelephony;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iTelephony;
    }
}

android的新版本已经把Phone类给隐藏起来了,想要用代码实现挂断电话,就必须通过AIDL才行,

第一步:在程序中新建一个包,包名必须为:com.android.internal.telephony,因为要使用aidl,

第二步:在这个包里面新建一个名为ITelephony.aidl的文件,然后在文件里面写入代码:

package com.android.internal.telephony;
interface ITelephony{
boolean endCall();
void answerRingingCall();
}

然后保存,eclipse会自动在gen文件夹下生成一个ITelephony.java的类。


package com.test.testcall;

import java.lang.reflect.Method;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.ITelephony;
import android.content.Context;

public class MainActivity extends ActionBarActivity {

    private   ITelephony iPhoney=null;
    private TelephonyManager manager;

    private TextView textContent;
    private Button btn;

    private boolean runnable=true;
    private boolean endCalls=false;
    String tempStr;
    Thread t=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitView();
    }

    void InitView(){
         textContent = (TextView)findViewById(R.id.textView2);
         btn = (Button)findViewById(R.id.button1);
         final TelephonyManager tm=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
         iPhoney=getITelephony(this);//获取电话实例



         btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                textContent.setText("18591939576");
                tempStr = textContent.getText().toString();
                if(tempStr.length() != 0){
//                  Intent phoneIntent = new Intent("android.intent.action.CALL",Uri.parse("tel:" + tempStr));
//                  startActivity(phoneIntent);
                    t.start();
                }else{
                    Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_LONG).show();
                }
            }
         });

         t=new Thread(new Runnable() {

               @Override
               public void run() {
               try {
                  while(runnable){

                Thread.sleep(5000);//延时5s
                int state=tm.getCallState();
                if(state==TelephonyManager.CALL_STATE_IDLE){
                 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tempStr));
                 startActivity(intent);
                }

                if(state==TelephonyManager.CALL_STATE_OFFHOOK){
                 Thread.sleep(50000);
                 endCalls= iPhoney.endCall();
                 InitView();
                 //System.out.println("是否成功挂断:"+endCall);
                }
//              
//              if(state==TelephonyManager.CALL_STATE_RINGING){
//                  System.out.println("Test ringing ringing ringing");
                 Thread.sleep(60000);
                 endCalls= iPhoney.endCall();
                 InitView();
//              }


                  }
               } catch (Exception e)
                  {
                e.printStackTrace();
                  }
               }
              });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 通过反射得到实例
     * @param context
     * @return
     */
    private static ITelephony getITelephony(Context context) {
        TelephonyManager mTelephonyManager = (TelephonyManager) context
                .getSystemService(TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method getITelephonyMethod = null;
        try {
            getITelephonyMethod = c.getDeclaredMethod("getITelephony",
                    (Class[]) null); // 获取声明的方法
            getITelephonyMethod.setAccessible(true);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        ITelephony iTelephony=null;
        try {
             iTelephony = (ITelephony) getITelephonyMethod.invoke(
                    mTelephonyManager, (Object[]) null); // 获取实例
            return iTelephony;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return iTelephony;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值