-1

我是 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 已停止”任何建议将不胜感激。谢谢

4

1 回答 1

0

检查你的清单。

你有没有把这个权限

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

?

于 2014-01-29T11:17:09.423 回答