将预定义消息插入现有对话(Android)

时间:2012-06-27 21:37:18

标签: android sms broadcastreceiver

我是否可以通过Android平台上构建的应用程序将预定义消息插入收件箱中的现有会话?

使用此代码,我可以检索最后发送和接收的消息。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);

Uri uriSMS = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
cur.moveToNext();

String body = cur.getString(cur.getColumnIndex("body"));   
String add = cur.getString(cur.getColumnIndex("address"));
String time = cur.getString(cur.getColumnIndex("date"));
String protocol = cur.getString(cur.getColumnIndex("protocol"));       

String contactName = "";
Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));  
Cursor c = getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );  
if( c.moveToFirst() ) {  
             int nameIndex = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
             contactName = c.getString(nameIndex);
}
c.close();
cur.close();

String out = "";
Date d = new Date(Long.valueOf(time)); 

if (protocol == null)
    out = "Sending to: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";
else 
    out = "Received from: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";

tv.setText(out);
setContentView(tv);

}

有什么办法可以将它合并到我的广播接收器(SmsReceiver.java)中吗?希望有一种方法可以触发事件,以便在收到新消息时,我可以在与收件箱中最新文本消息相关的对话中插入预定义消息。

我在看它的时候。有没有办法准确地检索本机电话号码而不要求用户手动输入?

1 个答案:

答案 0 :(得分:2)

使用我的接收器中的以下代码解决。

ContentValues values = new ContentValues();
 values.put("address", "0123456789");
 values.put("body", "Message goes here");
  context.getContentResolver().insert(Uri.parse("content://sms/"), values);
相关问题