Android短信应用程序我没有在手机上存储已发送的消息

时间:2011-05-27 13:06:42

标签: android sms store

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

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将sms消息存储在sqlite数据库中(这可能是完全错误的)我假设我需要将发送的消息传递给它但谷歌并不是我的朋友...请帮忙!

1 个答案:

答案 0 :(得分:0)

检查这个答案:

Android : Sending an SMS (using the Outbox)

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