2010-07-22 12 views
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); 
    } 
}); 
} 
} 

回答

5

您好,當我在我的 Android模擬器發送短信,它進入 內容提供商:內容://短信/發送 吧?

不一定。您認爲內容提供商存在於所有設備上,並被所有SMS客戶端應用程序使用。這些都不是有效的假設。

所以我想從內容提供商那裏得到最後發送的短信 。

That content provider is not part of the Android SDK.

+0

那麼什麼樣的內容,存在於所有的設備和所使用的是建議我用所有SMS客戶端應用程序,因爲這一次我提到是不是框架的一部分提供。我應該將哪個URI傳遞給query()方法? – rogcg 2010-07-23 01:25:11

+0

@psyhclo:沒有「內容提供者存在於所有設備中,並且被所有的SMS客戶端應用程序使用」,而不是爲桌面編寫的每個電子郵件應用程序都使用Outlook API。每個電子郵件應用程序(如果它具有一個API)都有其自己的API。同樣,每個SMS客戶端應用程序(如果它具有一個API)都有其自己的API。 – CommonsWare 2010-07-23 01:53:42

+0

那麼,我打算做什麼?如果我按照這樣的方式放棄,我的應用程序將面臨嚴重問題。對?我該怎麼辦? – rogcg 2010-07-23 01:57:04