gpt4 book ai didi

java - 无法读取 BroadcastReceiver 中的最后一条短信

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

我想编写一个简单的应用程序,在电话铃声响起时显示 toast 。该 toast 的文本是来电号码的最后一条短信。当电话响起时,我的 toast 工作正常,但当我想将 toast 文本设置为该号码的最后一条短信时,它不显示任何 toast ?!这是我的代码:

public class callReciver extends BroadcastReceiver {

private Toast toast;

@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
toast = new Toast(context);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

toast.setText(getsmsContent(incomingNumber, context));
toast.setDuration(600000);
toast.show();

} else {
toast.cancel();
}
}

public String getsmsContent(String number, Context con) {

int i = 0;
String num = null;
boolean Break = false;
String smsContent = null;
Cursor cursor = con.getApplicationContext().getContentResolver()
.query(Uri.parse("content://sms/"), null, null, null, null);

cursor.moveToFirst();
do {
num = cursor.getString(2);
if (num.equals(number)) {
smsContent = cursor.getString(5);
Break = true;
}
} while (cursor.moveToNext() && Break);
return smsContent;
}
}

list .xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.calltoast2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SMS"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.calltoast2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name="callReciver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>

</manifest>

最佳答案

正如评论中提到的,您希望将查询限制在收件箱中,并且不应该使用游标索引常量。此外,如果我们查询号码,我们可以完全消除循环。

public String getSmsContent(String number, Context context)
{
String smsContent = "No entry for " + number;

Cursor cursor = context.getContentResolver()
.query(Uri.parse("content://sms/inbox"), new String[]{"body"}, "address=?", new String[]{number}, null);

if(cursor != null && cursor.moveToFirst())
{
smsContent = cursor.getString(cursor.getColumnIndex("body"));
}

return smsContent;
}
<小时/>

经过进一步测试,SMS 提供商中存储的号码可能与从来电收到的号码不完全匹配。在这种情况下,我相信我们将不得不诉诸循环来检查可能的匹配。在这里,我们使用 PhoneNumberUtils.compare() 方法,如果号码“对于来电显示而言足够相同”,则该方法将返回 true。

public String getSmsContent(String number, Context context)
{
Cursor cursor = context.getContentResolver()
.query(Uri.parse("content://sms/inbox"), new String[]{"address", "body"}, null, null, null);

if (cursor != null && cursor.moveToFirst())
{
do {
if(PhoneNumberUtils.compare(number, cursor.getString(cursor.getColumnIndex("address"))))
{
return cursor.getString(cursor.getColumnIndex("body"));
}
} while(cursor.moveToNext());
}

return "No entry for " + number;
}

关于java - 无法读取 BroadcastReceiver 中的最后一条短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25916099/

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