2014-03-13 4 views
0

단추를 누르고 타이머가 0이되면 SMS와 전자 메일이 저장된 연락처로 보내고 메시지에 기본 설정에 저장된 정보가 들어있는 응용 프로그램을 코딩합니다. 나는 이메일 벌금과 SMS없이 충돌로 작동하는 것 같군 전송을하지만 난 전혀 SMS 수신하지 않습니다 : 사용자가 수신하는 방송 수신기가 필요SMS 활동이 보내지지 않습니다.

@Override

 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

도움이 될지 모르겠지만 도움이 될만한지 잘 모르겠다. –

답변

0

당신의 SMS. 코드는 다음과 같다 :

공용 클래스 SmsReceiver가 브로드 캐스트 리시버 { @Override 공공 무효 onReceive (컨텍스트 컨텍스트, 의도 의도를) 확장 { 가 // --- SMS 메시지가에 --- 번들 전달받을 번들 = intent.getExtras();
SmsMessage [] msgs = null; String 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(); 
    }       
} 

} 활동 클래스에서

는 의도가 나타납니다

,369 : 공용 클래스 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; 
} 

}

추가 권한 '활동을 확장

관련 문제