Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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("
onCallStateChanged: %s",stateS));

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

check your manifest.

Did you put this permission

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

?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...