SHARE
TWEET

Untitled

a guest May 23rd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.i.envoisms;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.support.v4.app.ActivityCompat;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.telephony.SmsManager;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.     private EditText edNum,edCorps;
  16.     private Button btnEnvoi;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.         init();
  23.     }
  24.  
  25.     private void init() {
  26.         edCorps=(EditText)findViewById(R.id.edCorps);
  27.         edNum=(EditText)findViewById(R.id.edNum);
  28.         btnEnvoi=(Button)findViewById(R.id.btnEnvoi);
  29.         ajouterEcouteur();
  30.     }
  31.  
  32.     private void ajouterEcouteur() {
  33.         btnEnvoi.setOnClickListener(new View.OnClickListener() {
  34.             @Override
  35.             public void onClick(View v) {
  36.                 envoi();
  37.             }
  38.         });
  39.     }
  40.     @Override
  41.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
  42.     {
  43.         if(requestCode == 101)
  44.         {
  45.             if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
  46.             {
  47.                 envoi();
  48.             }
  49.         }
  50.     }
  51.  
  52.     private void envoi() {
  53.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
  54.             // TODO: Consider calling
  55.             //    ActivityCompat#requestPermissions
  56.             // here to request the missing permissions, and then overriding
  57.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  58.             //                                          int[] grantResults)
  59.             // to handle the case where the user grants the permission. See the documentation
  60.             // for ActivityCompat#requestPermissions for more details.
  61.             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 101);
  62.             return;
  63.         }
  64.         SmsManager smgr = SmsManager.getDefault();
  65.         smgr.sendTextMessage(edNum.getText().toString(),null,edCorps.getText().toString(),null,null);
  66.         Toast.makeText(MainActivity.this, "SMS Sent Successfully", Toast.LENGTH_SHORT).show();
  67.     }
  68.  
  69.  
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top