Envio de SMS funciona no emulador e não celular!

1 resposta
Rafinha2108

Neste app que criei o envio do sms ocorre normal no emulador(em que envia um sms para outro emulador instanciado), problema e que ao testar em meu aparelho não funiona o envio!

Será que é porque meu aparelho é dois chips?

import br.com.(**************).R;
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.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.net.Uri;
import android.content.ContentValues;

public class Sincronia extends Activity implements View.OnClickListener{

    Button enviar;
    EditText edtNumero;
    EditText edtText;
    SmsManager smsManager;
    PendingIntent sendPI;
    PendingIntent deliveredPI;

    BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;

    private String SENT = "SMS_SENT";
    private String DELIVERED = "SMS_DELIVERED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.sincronia);
        initComponentes();
        enviar.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        smsSentReceiver=new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getApplicationContext(), "SMS has been sent", Toast.LENGTH_LONG).show();
                    Log.i("SEND","SMS has been sent");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getApplicationContext(), "Generic Failure", Toast.LENGTH_LONG).show();
                    Log.i("SEND","Generic Failure");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getApplicationContext(), "No Service", Toast.LENGTH_LONG).show();
                    Log.i("SEND","No Service");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getApplicationContext(), "Null PDU", Toast.LENGTH_LONG).show();
                    Log.i("SEND","Null PDU");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getApplicationContext(), "Radio Off", Toast.LENGTH_LONG).show();
                    Log.i("SEND","Radio Off");
                    break;
                default:
                    Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_LONG).show();

                }

            }
        };

        smsDeliveredReceiver=new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                switch(getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getApplicationContext(), "SMS Delivered", Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getApplicationContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        };

        registerReceiver(smsSentReceiver, new IntentFilter(SENT));
        registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));

    }

    public void onPause() {
        super.onPause();
        unregisterReceiver(smsSentReceiver);
        unregisterReceiver(smsDeliveredReceiver);
    }


    /**
     * Inicializa os componentes da janela
     */
    public void initComponentes(){
        enviar = (Button) findViewById(R.id.btSinc);
        edtNumero = (EditText) findViewById(R.id.sincedtnumero);
        edtText = (EditText) findViewById(R.id.sincedttext);
    }

    public boolean envio(String numero,String mensagem){
        try{
            //Inicializa
            SmsManager smsManager = SmsManager.getDefault();
            sendPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0);
            deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

            if(PhoneNumberUtils.isWellFormedSmsAddress(numero)){
                Log.i("OK","Feito 1");
                smsManager.sendTextMessage(numero,null,mensagem,sendPI,deliveredPI);
                return true;
            }else{
                Toast.makeText(this,"Falha no SMS - tente novamente!",Toast.LENGTH_LONG).show();
                return false;
            }

        }catch(Exception e){
            Toast.makeText(this,"FALHA",Toast.LENGTH_LONG).show();
            return false;
        }
    }
    public void salvaMensagem(String numero,String mensagem){
        ContentValues values = new ContentValues(); 
        values.put("address",numero); 
        values.put("body",mensagem);
        getContentResolver().insert(Uri.parse("content://sms/sent"), values);
    }
    @Override
    public void onClick(View v) {
        if(envio(edtNumero.getEditableText().toString(),edtText.getEditableText().toString())){
            salvaMensagem(edtNumero.getEditableText().toString(),edtText.getEditableText().toString());
        }

    }
}

1 Resposta

A

cola ai o manifest.xml, pode estar faltando alguma coisa

Criado 19 de fevereiro de 2014
Ultima resposta 13 de mar. de 2014
Respostas 1
Participantes 2