我想写一个简单的应用程序,在手机响铃时显示祝酒词。那个吐司的文字是来电号码的最后一个短信。我的吐司在电话铃声响起的时候工作得很好但是当我想设置吐司文字以持续该号码的短信时,它不会显示任何吐司? 有我的代码:
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;
}
}
manifest.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>
答案 0 :(得分:1)
正如评论中所提到的,您希望将查询限制在收件箱中,并且您不应该使用常量作为Cursor索引。此外,如果我们查询数字,我们可以完全消除循环。
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()
方法,如果数字与#34;相同,则返回true,以便用于来电显示。&#34;
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;
}