短信活动不会发送

时间:2014-03-13 19:52:15

标签: android

我编写了一个应用程序,当按下按钮并且计时器到达零时,SMS和电子邮件将发送给已保存的联系人,并且该消息包含保存在首选项中的信息。我有电子邮件发送正常,短信似乎没有崩溃,但我根本没有收到任何短信:

@覆盖

        public void onFinish() {
                final String[] personalInfo = db.getPersonalDetails();
                final Cursor contacts = db.getContacts();

                if (match == false) {
                    sendSms();

                    if (db.hasGmail()) {
                        Thread s = new Thread(new Runnable() {

                            public void run() {
                                String args[] = db.getGmail();
                                GmailSender sender = new GmailSender(args[0],args[1], getApplicationContext());

                                Cursor c = db.getEmailContacts();
                                while (c.moveToNext()) {
                                    try {

                                        Log.e(args[0], args[1]);
                                        sender.sendMail(
                                                args[0],
                                                c.getString(c
                                                        .getColumnIndex("emailAddress")));
                                    } catch (Exception e) {
                                        Log.e("SendMail", e.getMessage(), e);
                                    }
                                }
                            }

                        });
                        s.start();

                    }
                    Toast.makeText(getApplicationContext(), "Information sent",
                            5000).show();
                }
            }
        }.start();
    }
private void sendSms() {
        sms = new Intent(this, SMS.class);
        this.startService(sms);

    }

SMS Class:

public class SMS extends Service {

    String BankAccount, BankNameAddress, SortCode;
    String message;
    SharedPreferences prefs;

    public void initilizePrefs() {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        BankAccount = prefs.getString("BankAccount", null);
        BankNameAddress = prefs.getString("BankNameAddress", null);
        SortCode = prefs.getString("SortCode", null);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startid) {
        super.onStart(intent, startid);
        initilizePrefs();

        String mes = "my account info is: " + BankNameAddress + " "
                + " account number: " + BankAccount + " Sort Code is: "
                + SortCode + " " + "Thank you so much!!";

        try {
            if (BankNameAddress != null && BankAccount != null
                    && SortCode != null) {
                sendSMS("Help!! I've completely run out of money and need you to send some via bank transfer please. "
                        + mes);
            }

            else
                Toast.makeText(getBaseContext(),
                        "Please ensure all sections of preferences are filled",
                        Toast.LENGTH_SHORT).show();
        }

        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void sendSMS(String message) {
        Database db = new Database(this);
        Cursor cursor = db.getNumbers();
        db.onStop();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String phoneNumber = cursor.getString(cursor
                        .getColumnIndex("number"));
                Log.e("number", phoneNumber);
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, null, null);

1 个答案:

答案 0 :(得分:0)

您需要有一个接收短信的广播接收器。代码如下:

公共类SmsReceiver扩展了BroadcastReceiver {     @覆盖     public void onReceive(Context context,Intent intent)        {          // ---获取传入的短信---         Bundle bundle = intent.getExtras();
        SmsMessage [] msgs = null;         String str ="&#34 ;;

    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");//it must be given as it is only
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);   //Create an SmsMessage from a raw PDU.             
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---

        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("message", str);
        context.startActivity(i);
        Toast.makeText(context, "mmmm"+str, Toast.LENGTH_SHORT).show();
    }                         
}

}

在您的活动课程中,收到意图: 公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tx=(TextView) findViewById(R.id.textView1);
    Bundle bun=getIntent().getExtras();
    String stuff=bun.getString("message");
    tx.setText("Welcome,"+stuff);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

添加permisions: