我有一个应用程序可以在手机收到短信时处理联系人信息,我需要将联系人信息获取到我创建的短信接收器类中,但问题是当短信接收器获取联系人时出现我的空指针异常错误,因为它不要获取联系人信息,我的想法是获取所有联系人信息以比较 SMS 发件人的号码以获取他的 ID 以在我的应用程序中执行其他操作。
这是我的代码:
public class SMSBroadcastReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";
private static Activity mActivity;
DBHelper mDBHelper;
private Context mContext;
private ArrayList<Contact> mContactsArrayList;
public String str4;
private ContactManager aContactManager;
private ArrayList<Contact> ctnList;
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Log.d("SMSBroadcastReceiver", "Yes it calls the onReceive");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String str2 = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str2=msgs[i].getOriginatingAddress();
str = msgs[i].getMessageBody().toString();
str4 = str2;
}
if(str.charAt(0)=='#'&&str.length()<=4){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notManager =
(NotificationManager) context.getSystemService(ns);
//Configuramos la notificaci�n
int icono = R.drawable.resultadosmnu;
CharSequence textoEstado = "Ask-It notification";
long hora = System.currentTimeMillis();
Notification notif =
new Notification(icono, textoEstado, hora);
CharSequence titulo = "New set of response on Askit";
CharSequence descripcion = "There's available a new set of response on your Survey_Name graph";
Intent notIntent = new Intent(context,
ProyectoAskitActivity.class);
PendingIntent contIntent = PendingIntent.getActivity(
context, 0, notIntent, 0);
notif.setLatestEventInfo(
context, titulo, descripcion, contIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notManager.notify(icono, notif);
Contact cnt = queryDetailsForContactNumber(str2);
Toast toast1 =
Toast.makeText(context,
cnt.getPhoneNumber(), Toast.LENGTH_SHORT);
toast1.show();
}
}
}
public Contact queryDetailsForContactNumber(String aContactId) {
final String[] projection = new String[] {
Phone._ID, // the contact id column
Phone.DISPLAY_NAME, // the name of the contact
Phone.NUMBER // the id of the column in the data table for the image
};
final Cursor contact = mActivity.managedQuery(
Phone.CONTENT_URI,
projection,
Phone.NUMBER+"="+aContactId, // filter entries on the basis of the contact id
null, // the parameter to which the contact id column is compared to
null);
if(contact.moveToFirst()) {
final int contactId = contact.getInt(
contact.getColumnIndex(Phone._ID));
final String name = contact.getString(
contact.getColumnIndex(Phone.DISPLAY_NAME));
final String number = contact.getString(
contact.getColumnIndex(Phone.NUMBER));
final Contact aContact = new Contact(contactId, name,null,false,number);
return aContact;
}
return null;
}
}