2011-08-24 14 views
12

Próbuję utworzyć aplikację pod numerem READ PHONE STATE, a po zmianie stanu telefonu na wyświetlany jest stan Toast z bieżącym stanem. Ale po uruchomieniu aplikacja zatrzymuje się niespodziewanie.Android CZYTA SIĘ TELEFONEM?

moja klasa:

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

mój manifest jest:

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

Mój układ jest:

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

Będziesz potrzebować opublikować jakiś kod lub bardziej szczegółowo z tym, co próbujesz zrobić. Nie jesteśmy tutaj, aby napisać twój projekt dla ciebie. – Codeman

+0

Może być wyjątek SecurityException, ponieważ zapomniałeś dodać uprawnienia READ_PHONE_STATE w pliku AndroidManifest.xml –

Odpowiedz

29

nie widzę <uses-permission android:name="android.permission.READ_PHONE_STATE" /> w pliku manifestu.

Jest to wymagane, aby aplikacja mogła odczytać ten stan.

+0

wspaniałą odpowiedź! czy możesz mi powiedzieć, jak mogę sprawić, by moja aplikacja działała w tle po włączeniu telefonu ?! – AndBegginer

+1

Jeśli Twoja aplikacja powinna działać w tle, być może chcesz wykonać Usługę zamiast działania: http://developer.android.com/reference/android/app/Service.html - Mówię może tak, jak ja nie wiem • mieć wszystkie elementy kontekstowe potrzebne do określenia tego. – Shlublu

+2

Aby rozpocząć korzystanie z telefonu, użyj odbiornika transmisji BOOT_COMPLETED i uruchom z niego usługę. – marcinj

1

Aplikacja wie PHONE STATE dzięki zamiarem, która jest nadawana przez Telefonia Usługa powiadamiająca o wniosku o zmianach PHONE STATE.
Może trzeba linii prowadzącej do tworzenia aplikacji

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

Dodaj komentarz do tego kodu ... –

Powiązane problemy