gpt4 book ai didi

android - 收到短信时联系人姓名未显示在通知中

转载 作者:行者123 更新时间:2023-11-30 01:20:31 25 4
gpt4 key购买 nike

我正在制作一个应用程序,当从已知号码接收到新短信时,即如果该号码保存在联系人列表中,则显示推送通知。现在的问题是通知没有显示联系人的显示名称。它可以显示消息编号和消息内容,但不显示该联系人的显示名称。

我检查新短信是否来自保存的联系人的代码是:

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”而不显示实际名称请帮忙!

这是该类(class)的所有代码

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;
}

现在有两种方法,一种是检查是否存在,另一种是返回名字

最佳答案

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 是否不是

String title = contactExists(context, msg_from);

if (title != null) {
NotificationCompat.Builder notify = ...
...
}

您可能还想更改 contactExists() 方法名称,因为它现在返回显示名称。

关于android - 收到短信时联系人姓名未显示在通知中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37181243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com