Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android READ PHONE STATE?

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>
like image 403
AndBegginer Avatar asked Aug 24 '11 16:08

AndBegginer


People also ask

What is the use of read phone state permission?

READ_PHONE_STATE is one of the Android permissions categorized as dangerous. This is because it “allows read only access to phone state, including the phone number of the device, current cellular network information, the status of any ongoing calls, and a list of any Phone Accounts registered on the device” [2] .

What is phone state permission in Android?

The android.permission.READ_PHONE_STATE or the “Read Phone State and Identity” permission can be used to do a range of tasks, from simple things to quite invasive: - turning an application's sound off when a call comes in. - verifying the user/phone with IMEI information.

Why do Android apps need to read phone state and identity?

Its a permission that is being requested by an app for unique identification of your device. Allow it only of you are ok with it. As per Google, this permission is for getting the call status if it is active or not, current cellular network information and similar stuff. So, it depends on the type of app you're using.


3 Answers

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.

like image 159
Shlublu Avatar answered Sep 27 '22 00:09

Shlublu


<?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>
like image 24
Neel Agarwal Avatar answered Sep 27 '22 00:09

Neel Agarwal


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

  • Intent : see http://developer.android.com/reference/android/content/Intent.html for details and see http://developer.android.com/guide/topics/intents/intents-filters.html for concept and TelephonyManager.ACTION_PHONE_STATE_CHANGED is the name of the intent you need to receives thanks your BoradCastReceiver
  • BroadcastReceiver http://developer.android.com/reference/android/content/BroadcastReceiver.html and see http://developer.android.com/guide/topics/fundamentals.html at "Application Component" Section
  • android.permission.READ_PHONE_STATE permission have to be added in your AndroidManifest.xml file (here an example extract..)

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    
         package="xyz...."
         android:versionCode="1" android:versionName="0.1">
         <uses-sdk android:minSdkVersion="7" />
         <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    
     ...
    </manifest>
    
like image 30
Emmanuel Devaux Avatar answered Sep 25 '22 00:09

Emmanuel Devaux