ANDroid开发 - 通话状态

时间:2012-08-08 02:32:53

标签: android

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

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已经停止” 任何建议将被认真考虑。 THX

1 个答案:

答案 0 :(得分:0)

检查你的清单。

你有没有提出这个许可

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