package ghj.com.messageintent;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editBody = (EditText)findViewById(R.id.editBody);
Button btnSms = (Button)findViewById(R.id.btnSms);
btnSms.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
sendSmsIntent("010-1234-1234");
}
});
Button btnMms = (Button)findViewById(R.id.btnMms);
btnMms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getImage();
}
});
Button btnEmail = (Button)findViewById(R.id.btnEmail);
btnEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail("ghj@naver.com");
}
});
}
public void sendSmsIntent(String number){
try{
Uri smsUri = Uri.parse("sms:"+number);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
sendIntent.putExtra("sms_body", editBody.getText().toString());
startActivity(sendIntent);
// Intent sendIntent = new Intent(Intent.ACTION_VIEW);
// sendIntent.putExtra("address", number);
// sendIntent.putExtra("sms_body", editBody.getText().toString());
// sendIntent.setType("vnd.android-dir/mms-sms");
// startActivity(sendIntent);
}catch (Exception e){
e.printStackTrace();
}
}
public void sendMmsIntent(String number, Uri imgUri){
try{
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("address", number);
sendIntent.putExtra("subject", "MMS Test");
sendIntent.putExtra("sms_body", editBody.getText().toString());
sendIntent.setType("image/*");
sendIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.app_name)));
}catch (Exception e){
e.printStackTrace();
}
}
public void getImage(){
try{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Image Choose"), 1);
}catch (Exception e){
e.printStackTrace();
}
}
public void sendEmail(String email){
try{
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_CC, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_BCC, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email Test");
emailIntent.putExtra(Intent.EXTRA_TEXT, editBody.getText().toString());
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Email Choose"));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1){
if(resultCode==RESULT_OK){
Uri imgUri = data.getData();
sendMmsIntent("010-1234-1234", imgUri);
}
}
}
}