我在我的Android应用程序中使用PhoneState Action,但是有一个问题,我在振铃状态下从来电中得到号码,它工作正常但是在我接听电话后我的BroadCastReceiver崩溃,因为意图束是null因为那里不是要取的号码。
如何确保我只收听Ringing状态,而不是其他任何奇怪的东西:
这是我的代码:
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"/>
答案 0 :(得分:2)
有关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 :(得分:1)
您可以通过查看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