2011-08-24 16 views
12

私はアプリをREAD PHONE STATEにしようとしていて、電話の状態が現在の状態のToastと表示されるように変更されています。しかし、私はそれを起動すると、アプリは予期せず停止します。Android READ PHONE STATE?

私のクラス:

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

私のマニフェストは次のとおりです。

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

私のレイアウトは次のとおりです。

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

あなたは何かしようとしているいくつかのコードを投稿するか、より詳細にする必要があります。私たちはあなたのためにあなたのプロジェクトを書くためのものではありません。 – Codeman

+0

AndroidManifest.xmlファイルのREAD_PHONE_STATE権限を忘れたためにSecurityExceptionがスローされることがあります –

答えて

29

私はあなたのマニフェストファイルに<uses-permission android:name="android.permission.READ_PHONE_STATE" />を見ていません。

アプリケーションがその状態を読み取ることができるようにする必要があります。

+0

素晴らしい答え!電話がオンになったときに、アプリをバックグラウンドで実行させるにはどうすればいいのか教えてください。 – AndBegginer

+1

アプリをバックグラウンドで実行する必要がある場合は、アクティビティではなくサービスを提供したいと思うかもしれません。http://developer.android.com/reference/android/app/Service.html - これを決定するために必要なすべてのコンテキスト要素を持っています。 – Shlublu

+2

電話電源をオンにするには、BOOT_COMPLETEDブロードキャストレシーバーを使用し、そのサービスを開始します。 – marcinj

1

アプリケーションはPHONEのSTATEのおかげテレフォニーで放送された意図を知っていますPHONE STATEの変更についてアプリケーションに通知するサービス。
あなたが

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

このコードにコメントを追加してください... –