如何查询内容提供商最近发送的短信?

发布于 2024-09-11 11:51:07 字数 1421 浏览 1 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-09-18 11:51:07

您好,当我在我的手机上发送短信时
Android 模拟器,它转到
内容提供商:content://sms/sent
对吗?

未必。您假设内容提供程序存在于所有设备上并且由所有 SMS 客户端应用程序使用。这些都不是有效的假设。

所以我想获取最后发送的短信
来自内容提供商。

该内容提供程序不属于 Android SDK 的一部分。

Hello, when I send an SMS on my
Android emulator, it goes to the
content provider: content://sms/sent
right?

Not necessarily. You assume that content provider exists on all devices and is used by all SMS client applications. Those are not valid assumptions.

So I wanted to get the last sent SMS
from the content provider.

That content provider is not part of the Android SDK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文