仅听一个电话状态

问题描述

| 我在Android应用程序中使用Phonestate Action,但是有一个问题,Wen我在振铃状态期间从来电中获得号码,它可以正常工作,但是在我选择呼叫后,我的broadCastReceiver崩溃了,因为意图包在那里为空,因为没有数字可提取。 如何确定我仅在听振铃状态,而不听其他任何奇怪的声音: 这是我的代码
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);
        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\"/>
    

解决方法

从有关
EXTRA_INCOMING_NUMBER
的文档中:   与   ACTION_PHONE_STATE_CHANGED广播   对于包含传入的字符串   电话号码。仅在新时有效   通话状态为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);
                 cursor.moveToFirst();
                 int count=cursor.getCount();
                 Toast.makeText(context,String.valueOf(count),Toast.LENGTH_LONG).show();
             }
        }
    }
}
    ,您可以通过在onReceive上查看intent中的状态来确保仅侦听Ringing状态。
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     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...