Android开发 - 拨打电话的状态 [英] ANdroid Development - Call state of the phone

查看:170
本文介绍了Android开发 - 拨打电话的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,我需要阅读的手机通话状态。我收到错误,当应用程序运行时(停止):

I'm new to Android and I need to read the call state of the phone. I receive error when the app runs (stopped):

package com.example.droid1;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;
import android.widget.Toast;

public class DroidActivity extends Activity {
    private TextView text0;
    private TelephonyManager telephoneM;
    private PhoneStateListener listner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_droid);
        text0 = (TextView) findViewById(R.id.textout);
        telephoneM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        listner = new PhoneStateListener()
        {
            public void onCallStateChanged(int state, String incomingnumber) {
                String stateS = "N/A";
                switch(state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    stateS = "Oscioso";
                    Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    stateS = "Sonando";
                    Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    stateS = "Ocupado";
                    Toast.makeText(DroidActivity.this, ""+stateS,Toast.LENGTH_SHORT).show();
                    break;  
                }
                text0.append (String.format("\nonCallStateChanged: %s",stateS));

            }
        };
        telephoneM.listen(listner, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

我没有在Eclipse中的任何错误信息,应用程序无需安装虚拟设备上的问题,但是,当它运行我的错误消息不幸的是Droid1已经停止
任何意见将是AP preciated。 THX

I don't have any error messages in eclipse, the app installs without a problem on Virtual device but when it run I have the error message of "Unfortunatly Droid1 has stopped" Any advice will be appreciated. Thx

推荐答案

检查清单。

你把这个权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

这篇关于Android开发 - 拨打电话的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆