2014-03-13 61 views
0

1編碼的應用程序,當按下一個按鈕和一個計時器達到零,短信和電子郵件發送到保存的聯繫人,消息包含保存偏好的信息。我有電子郵件發送精細和短信似乎沒有崩潰,在工作,但我沒有收到所有任何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); 
+0

我不知道這是否會幫助你或沒有,但在onStart的其他缺少{}只是說... –

回答

0

,你需要有一個廣播接收器將接收您的短信 。代碼如下:

公共類SmsReceiver擴展廣播接收器 { @覆蓋 公共無效的onReceive(上下文語境,意圖意圖) {// ---獲取短信中--- 通過捆綁bundle = intent.getExtras();
SmsMessage []的MSG = NULL; 字符串str = 「」;

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擴展活動{

@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: