收到短信时,联系人姓名未显示在通知中
Posted
技术标签:
【中文标题】收到短信时,联系人姓名未显示在通知中【英文标题】:Contact name not showing in Notifications when Sms receives 【发布时间】:2016-05-12 08:24:10 【问题描述】:我正在制作一个应用程序,当收到来自已知号码的新短信时,即如果该号码保存在联系人列表中,则推送通知显示。现在的问题是通知没有显示联系人的显示名称。它可以显示消息编号和消息内容,但不显示该联系人的显示名称。
我检查新短信是否来自已保存联系人的代码是:
public boolean contactExists(Context context, String number)
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME ;
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try
if (cur.moveToFirst())
return true;
finally
if (cur != null)
cur.close();
return false;
通知提醒的代码是:
if (contactExists(context, msg_from))
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(msg_from);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
notify.setVibrate(new long[]1000, 1000, 1000, 1000, 1000);
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
//notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
现在你在这一行看到我写“msg_from”
notify.setContentTitle(msg_from);
这会给我消息来自的号码,但如果我写这个:
notify.setContentTitle(PhoneLookup.DISPLAY_NAME);
或我在互联网上找到的其他解决方案,然后显示的通知标题将显示此字符串“display_name”而不显示实际名称 请帮忙!
这是我该类的所有代码
public class TodoRe extends BroadcastReceiver
Context context;
ArrayList<String> keywordslist = new ArrayList<String>();
@SuppressLint( "DefaultLocale", "InlinedApi" )
@Override
public void onReceive(Context context, Intent intent)
LinkedHashMap<String, String> contactNumber = new LinkedHashMap<String, String>();
DBkeyword screenedKeywordDB = new DBkeyword(context);
SQLiteDatabase db = screenedKeywordDB.getWritableDatabase();
Cursor cur1 = db.rawQuery(Constants.readScreenedKeywords, null);
cur1.moveToFirst();
while (!cur1.isAfterLast())
keywordslist.add(cur1.getString(0));
cur1.moveToNext();
db.close();
DBTable dbtable = new DBTable(context);
SQLiteDatabase dbrsn = dbtable.getReadableDatabase();
Cursor cur = dbrsn.rawQuery(Constants.readScreenedNumbers, null);
cur.moveToFirst();
while (!cur.isAfterLast())
contactNumber.put(cur.getString(0), cur.getString(1));
cur.moveToNext();
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String msg_from,msgSenderName;
if (bundle != null)
try
boolean keywordPresent = false;
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]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
String title= cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
msg_from = Utilities.extractNumbers(msg_from);
Long dateLong = msgs[i].getTimestampMillis();
String msgDate = dateLong.toString();
ContentValues values = new ContentValues();
values.put("address", msg_from);
values.put("date", System.currentTimeMillis()+"");
values.put("read", "1");
values.put("type", "1");
values.put("body",msgBody);
Uri uri = Uri.parse("content://sms/");
context.getContentResolver().insert(uri,values);
if (contactExists(context, msg_from))
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(title);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
notify.setVibrate(new long[]1000, 1000, 1000, 1000, 1000);
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
//notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
public boolean contactExists(Context context, String number)
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME ;
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try
if (cur.moveToFirst())
return true;
finally
if (cur != null)
cur.close();
return false;
这是现在更新的代码
ContentValues values = new ContentValues();
values.put("address", msg_from);
values.put("date", System.currentTimeMillis()+"");
values.put("read", "1");
values.put("type", "1");
values.put("body",msgBody);
Uri uri = Uri.parse("content://sms/");
context.getContentResolver().insert(uri,values);
String title = contactName(context, msg_from);
String sss=cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
if (contactExists(context, msg_from) && title != null)
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.appicon);
notify.setContentTitle(sss);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
notify.setVibrate(new long[]1000, 1000, 1000, 1000, 1000);
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
//notify.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 268435456));
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
public boolean contactExists(Context context, String number)
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME ;
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try
if (cur.moveToFirst())
return true;
finally
if (cur != null)
cur.close();
return false;
public String contactName(Context context, String number)
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME ;
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try
if (cur.moveToFirst())
return cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME ));
finally
if (cur != null)
cur.close();
return null;
现在有两种方法,一种检查是否存在,另一种返回名称
【问题讨论】:
【参考方案1】:DISPLAY_NAME
常量是保存联系人显示名称的数据库表列的名称。它的值是"display_name"
,这就是为什么您会在Notification
中看到它。
您需要Cursor
对该列的值。也就是说,你想要:
cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME))
更改contactExists()
方法以返回String
作为显示名称,而不是仅指示它是否存在的boolean
。
public String contactExists(Context context, String number)
...
try
if (cur.moveToFirst())
return cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME ));
finally
if (cur != null)
cur.close();
return null;
然后,将title
更改为该方法的返回值,并更改if
语句以检查title
是否不是null
。
String title = contactExists(context, msg_from);
if (title != null)
NotificationCompat.Builder notify = ...
...
您可能还想更改 contactExists()
方法名称,因为它现在返回显示名称。
【讨论】:
我写下这一行并将其保存在字符串名称“title”中,然后我写 notify.setContentTitle(title);但现在通知没有出现 您必须使用您对代码所做的更改来编辑您的问题。从您的评论中我无法判断它可能在哪里失败。 是的,你用错了Cursor
。您想更改 contactExists()
方法以返回显示名称,而不仅仅是 true
或 false
。给我一分钟,我会更新答案。
好的,先生,请检查一下
当短信收到时出现以下错误并且应用程序强制关闭以上是关于收到短信时,联系人姓名未显示在通知中的主要内容,如果未能解决你的问题,请参考以下文章