2011-05-25 3 views
0


私はAndroidアプリでPhoneStateアクションを使用していますが、問題があります。電話が鳴っている間に着信から電話番号を受け取ります。私のBroadCastReceiverは、インテントバンドルがヌルであるためにクラッシュします。フェッチする番号がありません。ただ1つの電話の状態を聞く

どのように私は私が唯一のリンギング状態ではなく、他の奇妙なものにに耳を傾けています確認してください:ここで

は私のコードです:ここでは

package com.messageHider; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 

public class incomingCallReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle=intent.getExtras(); 
     String number=bundle.getString("incoming_number"); 
     dbConnection connection=new dbConnection(context); 
     SQLiteDatabase db=connection.getReadableDatabase(); 
     Cursor cursor=db.query(dbConnection.TABLE_CONTACTS, null, dbConnection.CONTACT+"=?",new String[]{number}, null, null, null); 
     cursor.moveToFirst(); 
     int count=cursor.getCount(); 
     Toast.makeText(context,String.valueOf(count), Toast.LENGTH_LONG).show(); 
    } 
} 

は私のマニフェストです:

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".messageHider" 
       android:label="@string/app_name"> 

    </activity> 
    <activity android:name=".sms"/> 
    <activity android:name=".viewsms"/> 
    <activity android:name=".hiddenMessages"></activity> 
    <activity android:name=".viewContacts"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <service android:name=".smsService"/> 
    <receiver android:name=".smsReceiver"> 
     <intent-filter android:priority="100"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     </intent-filter> 
    </receiver> 
    <receiver android:name=".smsSentReceiver"> 
     <intent-filter> 
      <action android:name="android.provider.Telephony.SMS_SENT"/> 
     </intent-filter> 
    </receiver> 
    <receiver android:name=".incomingCallReceiver"> 
     <intent-filter android:priority="100"> 
      <action android:name="android.intent.action.PHONE_STATE"/> 
     </intent-filter> 
    </receiver> 
</application> 
<uses-permission android:name="android.permission.READ_SMS"/> 
<uses-permission android:name="android.permission.WRITE_SMS"/> 
<uses-permission android:name="android.permission.RECEIVE_SMS"/> 
<uses-permission android:name="android.permission.SEND_SMS"/> 
<uses-permission android:name="android.permission.READ_CONTACTS"/> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

答えて

2

投稿者tationについてEXTRA_INCOMING_NUMBER: ACTION_PHONE_STATE_CHANGEDで使用

検索キーは、着信 電話番号を含むStringため を放送します。新しい コール状態がRINGINGの場合にのみ有効です。

これはあなたのコードがなければならないことを意味し:

public class incomingCallReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) {    
     Bundle bundle=intent.getExtras(); 
     if(bundle != null){ 
      String number=bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
      if(number != null){ 
       dbConnection connection=new dbConnection(context); 
       SQLiteDatabase db=connection.getReadableDatabase(); 
       Cursor cursor=db.query(dbConnection.TABLE_CONTACTS, null, 
             dbConnection.CONTACT+"=?", 
             new String[]{number}, 
             null, null, null); 
       cursor.moveToFirst(); 
       int count=cursor.getCount(); 
       Toast.makeText(context,String.valueOf(count), Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 
1

あなたが意思の余分なあなたのonReceiveの状態を見て、あなたが鳴る状態にのみ聞くことができます:

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { 
    // the phone switched to the Ringing state 
} else { 
    // the phone switched to another state 
} 

Androidの料理の書籍の例をご覧ください:http://androidcookbook.com/Recipe.seam?recipeId=1109

関連する問題