2011-05-27 22 views
1

在我創建了一個短信應用程序,它不保存我送使用的應用程序中的消息!下面是發送部分的代碼:Android的短信應用我做不存儲發送信息的電話

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數據庫的短信(這可能是完全錯誤的),我想我需要在發送的消息傳遞給它但谷歌並不是我的朋友在這一個...請幫助!

+0

我具有這些權限: <使用的許可機器人:名稱= 「android.permission.SEND_SMS」/> \t \t <使用的許可機器人:名稱= 「android.permission.RECEIVE_SMS」/> \t \t <採用-許可的android:NAME = 「android.permission.READ_SMS」/> \t \t <使用許可權的android:NAME = 「android.permission.WRITE_SMS」/> – AndroidNoob 2011-05-31 08:54:11

回答

0

檢查這個答案:

Android : Sending an SMS (using the Outbox)

它有一個鏈接到一個樣本項目。

+0

感謝您的鏈接,但它看起來很奇怪,雖然這顯然你需要創建自己的發件箱,因爲您可以使用此代碼來獲取發件箱中的內容: '烏里allMessage = Uri.parse(「內容://短信/發送」) ; ContentResolver的CR = getContentResolver(); c = cr.query(allMessage,null,null,null,null);' 這個內容存儲在哪裏,爲什麼我不能添加它? – AndroidNoob 2011-06-01 09:27:02

+1

用google搜索'content:// sms/sent'後,我發現這個小寶石: 'private void storeMessage(String mobNo,String msg){ ContentValues values = new ContentValues(); values.put( 「地址」,mobNo); values.put(「body」,msg); 。 getContentResolver()插入(Uri.parse( 「內容:// SMS /發送」),值); }' 就像一個魅力!其實將你的短信儲存在發件箱中!有一個警告,但要記住,這種技術會在沒有消息應用程序的設備上中斷,並可能在Android的未來版本中崩潰。 – AndroidNoob 2011-06-01 10:05:01