web-dev-qa-db-fra.com

Android LIRE L'ÉTAT DU TÉLÉPHONE?

J'essaie de créer une application pour READ PHONE STATE et lorsque l'état du téléphone est modifié pour afficher Toast avec l'état actuel. Mais quand je le démarre, l'application s'arrête de façon inattendue.

ma classe :

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);
    }
}

mon manifeste est:

<?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>

Ma disposition est:

<?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>
15
AndBegginer

Je n'ai pas vu <uses-permission Android:name="Android.permission.READ_PHONE_STATE" /> dans votre fichier manifeste.

Il est nécessaire que votre application puisse lire cet état.

31
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>
3
Neel Agarwal

Votre application connaît PHONE STATE grâce à une intention qui est diffusée par le service de téléphonie notifiant à l'application les modifications de PHONE STATE.
Vous aurez peut-être besoin d'une ligne guide pour créer votre application

1
Emmanuel Devaux