2011-08-24 22 views
12

Estoy tratando de hacer la aplicación a READ PHONE STATE y cuando el estado del teléfono se cambia para mostrar Toast con el estado actual. Pero cuando lo comienzo, la aplicación se detiene inesperadamente.Android LEER TELÉFONO ESTADO?

mi clase:

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

mi manifiesto es:

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

Mi diseño es:

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

Necesitará publicar algún código o ser más detallado con lo que está tratando de hacer. No estamos aquí para escribir su proyecto para usted. – Codeman

+0

Puede ser una SecurityException lanzada porque olvidó agregar el permiso READ_PHONE_STATE en su archivo AndroidManifest.xml –

Respuesta

29

no vi <uses-permission android:name="android.permission.READ_PHONE_STATE" /> en su archivo de manifiesto.

Se requiere que su aplicación pueda leer ese estado.

+0

¡excelente respuesta! ¿Me puede decir cómo puedo hacer que mi aplicación se ejecute en segundo plano cuando el teléfono se enciende? – AndBegginer

+1

Si su aplicación se ejecuta en segundo plano, quizás esté dispuesto a hacer un Servicio en lugar de una Actividad: http://developer.android.com/reference/android/app/Service.html - Digo que tal vez no lo hago t tiene todos los elementos contextuales necesarios para determinar esto. – Shlublu

+2

Para iniciar el encendido del teléfono, use el receptor de transmisión BOOT_COMPLETED e inicie su servicio desde él. – marcinj

1

Su aplicación sabe TELÉFONO estado gracias una intención que se emite por la telefonía Servicio notificando a la aplicación sobre cambios en el ESTADO DEL TELÉFONO.
Es posible que necesite Guía de línea para crear su aplicación

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

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

    <application 
     android:icon="@drawable/icon"`enter code here` 
     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> 
+5

Por favor, agrega un comentario a este código ... –

Cuestiones relacionadas