Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
  4. Button send;
  5. EditText message;
  6. EditText phoneno;
  7. String number;
  8. String txt;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14.  
  15. send=(Button)findViewById(R.id.sendButton);
  16. message=(EditText)findViewById(R.id.textMessage);
  17. phoneno=(EditText)findViewById(R.id.phone);
  18.  
  19. send.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. sendSMSMessage();
  23. }
  24. });
  25. }
  26.  
  27. public void sendSMSMessage(){
  28. number=phoneno.getText().toString();
  29. txt=message.getText().toString();
  30.  
  31. if (ContextCompat.checkSelfPermission(this,
  32. Manifest.permission.SEND_SMS)
  33. != PackageManager.PERMISSION_GRANTED) {
  34. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  35. Manifest.permission.SEND_SMS)) {
  36. } else {
  37. ActivityCompat.requestPermissions(this,
  38. new String[]{Manifest.permission.SEND_SMS},
  39. MY_PERMISSIONS_REQUEST_SEND_SMS);
  40. }
  41. }
  42.  
  43. }
  44.  
  45. @Override
  46. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  47. //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  48. switch (requestCode) {
  49. case MY_PERMISSIONS_REQUEST_SEND_SMS: {
  50. if (grantResults.length > 0
  51. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  52. SmsManager smsManager = SmsManager.getDefault();
  53. smsManager.sendTextMessage(number, null, txt, null, null);
  54. Toast.makeText(getApplicationContext(), "SMS sent.",
  55. Toast.LENGTH_LONG).show();
  56. } else {
  57. Toast.makeText(getApplicationContext(),
  58. "SMS faild, please try again.", Toast.LENGTH_LONG).show();
  59. return;
  60. }
  61. }
  62. }
  63.  
  64. }
  65.  
  66. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  67. xmlns:app="http://schemas.android.com/apk/res-auto"
  68. xmlns:tools="http://schemas.android.com/tools"
  69. android:layout_width="match_parent"
  70. android:layout_height="match_parent"
  71. tools:context="com.example.shaloin.sample852.MainActivity">
  72.  
  73. <EditText
  74. android:id="@+id/textMessage"
  75. android:textSize="20sp"
  76. android:layout_marginTop="20dp"
  77. android:hint="Text Message"
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content" />
  80. <EditText
  81. android:id="@+id/phone"
  82. android:layout_below="@+id/textMessage"
  83. android:textSize="20sp"
  84.  
  85. android:layout_marginTop="20dp"
  86. android:hint="Phone Number"
  87. android:layout_width="match_parent"
  88. android:layout_height="wrap_content" />
  89. <Button
  90. android:id="@+id/sendButton"
  91. android:layout_below="@+id/phone"
  92. android:layout_centerHorizontal="true"
  93. android:text="Send"
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content" />
  96.  
  97. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  98. package="com.example.shaloin.sample852">
  99.  
  100. <uses-permission android:name="android.permission.SEND_SMS"/>
  101. <uses-permission android:name="android.permission.RECEIVE_SMS"/>
  102.  
  103. <application
  104. android:allowBackup="true"
  105. android:icon="@mipmap/ic_launcher"
  106. android:label="@string/app_name"
  107. android:roundIcon="@mipmap/ic_launcher_round"
  108. android:supportsRtl="true"
  109. android:theme="@style/AppTheme">
  110. <activity android:name=".MainActivity">
  111. <intent-filter>
  112. <action android:name="android.intent.action.MAIN" />
  113.  
  114. <category android:name="android.intent.category.LAUNCHER" />
  115. </intent-filter>
  116. </activity>
  117. </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement