如何查询内容提供商最近发送的短信?
当我在 Android 模拟器上发送短信时,它会发送至内容提供商:
content://sms/sent
对吗?
所以我想从内容提供商那里获取最后发送的短信。所以我使用了这个 Uri,正如您在上面看到的,我使用了方法查询和内容解析器对象。我得到了光标,并使用了 movetofirst() 方法,这样我就可以得到最后发送的短信。检查下面的代码。
package com.sys;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.net.Uri;
import android.database.Cursor;
public class SMS extends Activity {
Button btnVerSms;
EditText txtFinal;
final Uri CONTENT_URI = Uri.parse("content://sms/sent");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnVerSms= (Button)findViewById(R.id.btnVerSms);
txtFinal = (EditText)findViewById(R.id.txtFinal);
btnVerSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
String body = null;
if(cursor.moveToFirst()){
body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
}
txtFinal.setText(body);
}
});
}
}
When I send an SMS on my Android emulator, it goes to the content provider:
content://sms/sent
right?
So I wanted to get the last sent SMS from the content provider. So I used this Uri as you can see above and I used the method query, with the Content Resolver Object. And I got the cursor, and used the movetofirst() method, so I would have the last sent SMS. Check the code below.
package com.sys;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.net.Uri;
import android.database.Cursor;
public class SMS extends Activity {
Button btnVerSms;
EditText txtFinal;
final Uri CONTENT_URI = Uri.parse("content://sms/sent");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnVerSms= (Button)findViewById(R.id.btnVerSms);
txtFinal = (EditText)findViewById(R.id.txtFinal);
btnVerSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
String body = null;
if(cursor.moveToFirst()){
body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
}
txtFinal.setText(body);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未必。您假设内容提供程序存在于所有设备上并且由所有 SMS 客户端应用程序使用。这些都不是有效的假设。
该内容提供程序不属于 Android SDK 的一部分。
Not necessarily. You assume that content provider exists on all devices and is used by all SMS client applications. Those are not valid assumptions.
That content provider is not part of the Android SDK.