【问题标题】:Android SMS app I made not storing sent messages on the phone我制作的Android SMS应用程序不在手机上存储已发送的消息
【发布时间】:2011-09-03 09:41:17
【问题描述】:

我创建了一个 SMS 应用程序,但它没有存储我使用该应用程序发送的消息!下面是发送部分的代码:

package com.pearson.sms;

import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PearsonSMS extends Activity {
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;

String phoneNumber;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
    txtMessage = (EditText) findViewById(R.id.txtMessage);
    phoneNumber = "";

    try{
    /*Split the incoming uri to retrieve the phone number*/
    Uri urithing = getIntent().getData();
    phoneNumber = urithing.toString();
    String[] splitUri = phoneNumber.split(":");

    phoneNumber = splitUri[1];
    txtPhoneNo.setText(phoneNumber);
    }
    catch (Error e)
    {
        Toast.makeText(getBaseContext(), 
                "Error:"+e.getMessage(), 
                Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {
        Toast.makeText(getBaseContext(), 
                "Exception:"+e.getMessage(), 
                Toast.LENGTH_SHORT).show();
    }

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            Toast.makeText(getBaseContext(), "Sending SMS...", 
                    Toast.LENGTH_SHORT).show();
            String phoneNo = txtPhoneNo.getText().toString();
            String message = txtMessage.getText().toString();                 
            if (phoneNo.length()>0 && message.length()>0)                
                sendSMS(phoneNo, message);                
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    });        
}

private void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       
}
}

我在网上某处读到 android 将短信存储在 sqlite 数据库中(这可能完全错误)请帮忙!

【问题讨论】:

  • 我有这些权限:

标签: android sms store


【解决方案1】:

检查这个答案:

Android : Sending an SMS (using the Outbox)

它有一个示例项目的链接。

【讨论】:

  • 感谢您的链接,这似乎很奇怪,但显然您需要创建自己的发件箱,因为您可以使用此代码来获取发件箱的内容:Uri allMessage = Uri.parse("content://sms/sent"); ContentResolver cr = getContentResolver(); c = cr.query(allMessage, null, null, null, null); 此内容存储在哪里?为什么我不能添加它?!
  • 在谷歌搜索content://sms/sent 之后,我发现了这个小宝石:private void storeMessage(String mobNo, String msg) { ContentValues values = new ContentValues(); values.put("address", mobNo); values.put("body", msg); getContentResolver().insert(Uri.parse("content://sms/sent"), values); } 就像一个魅力!实际上将您的短信存储在发件箱中!但请记住,此技术将在没有消息应用程序的设备上中断,并可能在未来版本的 Android 上中断。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2015-09-23
  • 2011-03-16
  • 1970-01-01
相关资源
最近更新 更多