Loading...

Thursday, 9 August 2012

Send SMS (Android)

package app.test;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Test extends Activity {
  Button bt;

  String message;

  Boolean OnKeyTurn = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addKeyListener();

        BtnListener();

    }

    private void BtnListener(String message) {
        bt = (Button) findViewById(R.id.button1);   
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
              String[] to = {"w@l.net", "w@g.com"};
              String[] cc = {"c@l.net"};      
              sendEmail(to, cc, "Hello", message;);
            }
        });
    }

    //---sends an SMS message to another device---
    private void sendEmail(String[] emailAddresses, String[] carbonCopies,
    String subject, String message){
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        String[] to = emailAddresses;
        String[] cc = carbonCopies;
        emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent.putExtra(Intent.EXTRA_CC, cc);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, message);   
        emailIntent.setType("message/rfc822");
        startActivity(Intent.createChooser(emailIntent, "Email"));
    }

   private void addKeyListener(){

        edittext1 = (EditText) findViewById(R.id.editText1);
        edittext1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        message = edittext1.getText().toString();
                    OnKeyTurn = true;
                    return true; 
                }
                if(OnKeyTurn == false)
                    value = "";
                return false;
            }
        });

   }

}

No comments:

Post a Comment