17

I'm trying to make app to READ PHONE STATE and when the phone state is changed to display Toast with the current state. But when I start it, the app stops unexpectedly.

my class :

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

public class TelephonyDemo extends Activity {
    TextView textOut;
    TelephonyManager telephonyManager;
    PhoneStateListener listener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the UI
        textOut = (TextView) findViewById(R.id.textOut);

        // Get the telephony manager
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        // Create a new PhoneStateListener
        listener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                String stateString = "N/A";
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    stateString = "Idle";
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    stateString = "Off Hook";
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    stateString = "Ringing";
                    break;
                }
                textOut.append(String.format("\nonCallStateChanged: %s",
                        stateString));
            }
        };

        // Register the listener with the telephony manager
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

my manifest is :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.marakana"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >
        <activity
            android:name=".TelephonyDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="7" />

</manifest>

My layout is :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Telephony Demo"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/textOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Output" >
    </TextView>

</LinearLayout>
Piper
  • 1,266
  • 3
  • 15
  • 26
AndBegginer
  • 233
  • 2
  • 5
  • 9
  • You're going to need to post some code or be more detailed with what you're trying to do. We're not here to write your project for you. – Codeman Aug 24 '11 at 16:32
  • May be a SecurityException is thrown because you forgotten to add READ_PHONE_STATE permission in your AndroidManifest.xml file – Emmanuel Devaux Aug 24 '11 at 22:27

3 Answers3

34

I did not see <uses-permission android:name="android.permission.READ_PHONE_STATE" /> in your Manifest file.

It is required for your application to be able to read that state.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
  • great answer! can you tell me how can i make my app run in background when phone turned on?! – AndBegginer Aug 24 '11 at 22:25
  • 3
    If your app should run in the background, you are maybe willing to do a Service instead of an Activity: http://developer.android.com/reference/android/app/Service.html - I say maybe as I don't have all contextual elements needed to determine this. – Shlublu Aug 25 '11 at 06:24
  • 2
    To start on phone power on use BOOT_COMPLETED broadcast receiver, and start your service from it. – marcinj Jul 20 '13 at 06:17
3
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.marakana"
    android:versionCode="1"
    android:versionName="1.0" >

    /* permission should be added like below*/
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >
        <activity
            android:name=".TelephonyDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="7" />

</manifest>
anand krish
  • 4,281
  • 4
  • 44
  • 47
1

Your application knows PHONE STATE thanks an Intent that is Broadcasted by the Telephony Service notifying to application about PHONE STATE changes.
You may need Guide line to create your application

Garg
  • 2,731
  • 2
  • 36
  • 47
Emmanuel Devaux
  • 3,327
  • 4
  • 25
  • 30