over 9 years ago
If you want to create sms delivery app you can use my below code. In the below code I have used Intent , PandingIntent and Broadcastreceiver. In the below code I have also used Toast function to deliver SMS notification. See the below example it will clearly described how to make sent sms confirmation app.
- public class MainActivity extends Activity {
- Button btnSend;
- EditText etPhoneNo;
- EditText etMsg;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etPhoneNo = (EditText) findViewById(R.id.phno);
- etMsg = (EditText) findViewById(R.id.smstxt);
- btnSend = (Button) findViewById(R.id.send);
- btnSend.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String phoneNo = etPhoneNo.getText().toString();
- String msg = etMsg.getText().toString();
- try {
- String SENT = "sent";
- String DELIVERED = "delivered";
- Intent sentIntent = new Intent(SENT);
- /*Create Pending Intents*/
- PendingIntent sentPI = PendingIntent.getBroadcast(
- getApplicationContext(), 0, sentIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- Intent deliveryIntent = new Intent(DELIVERED);
- PendingIntent deliverPI = PendingIntent.getBroadcast(
- getApplicationContext(), 0, deliveryIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- /* Register for SMS send action */
- registerReceiver(new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String result = "";
- switch (getResultCode()) {
- case Activity.RESULT_OK:
- result = "Transmission successful";
- break;
- case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
- result = "Transmission failed";
- break;
- case SmsManager.RESULT_ERROR_RADIO_OFF:
- result = "Radio off";
- break;
- case SmsManager.RESULT_ERROR_NULL_PDU:
- result = "No PDU defined";
- break;
- case SmsManager.RESULT_ERROR_NO_SERVICE:
- result = "No service";
- break;
- }
- Toast.makeText(getApplicationContext(), result,
- Toast.LENGTH_LONG).show();
- }
- }, new IntentFilter(SENT));
- /* Register for Delivery event */
- registerReceiver(new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- Toast.makeText(getApplicationContext(), "Deliverd",
- Toast.LENGTH_LONG).show();
- }
- }, new IntentFilter(DELIVERED));
- /*Send SMS*/
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage(phoneNo, null, msg, sentPI,
- deliverPI);
- } catch (Exception ex) {
- Toast.makeText(getApplicationContext(),
- ex.getMessage().toString(), Toast.LENGTH_LONG)
- .show();
- ex.printStackTrace();
- }
- }
- });
- }
- @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;
- }
- }
public class MainActivity extends Activity { Button btnSend; EditText etPhoneNo; EditText etMsg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etPhoneNo = (EditText) findViewById(R.id.phno); etMsg = (EditText) findViewById(R.id.smstxt); btnSend = (Button) findViewById(R.id.send); btnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phoneNo = etPhoneNo.getText().toString(); String msg = etMsg.getText().toString(); try { String SENT = "sent"; String DELIVERED = "delivered"; Intent sentIntent = new Intent(SENT); /*Create Pending Intents*/ PendingIntent sentPI = PendingIntent.getBroadcast( getApplicationContext(), 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent deliveryIntent = new Intent(DELIVERED); PendingIntent deliverPI = PendingIntent.getBroadcast( getApplicationContext(), 0, deliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT); /* Register for SMS send action */ registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String result = ""; switch (getResultCode()) { case Activity.RESULT_OK: result = "Transmission successful"; break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: result = "Transmission failed"; break; case SmsManager.RESULT_ERROR_RADIO_OFF: result = "Radio off"; break; case SmsManager.RESULT_ERROR_NULL_PDU: result = "No PDU defined"; break; case SmsManager.RESULT_ERROR_NO_SERVICE: result = "No service"; break; } Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); } }, new IntentFilter(SENT)); /* Register for Delivery event */ registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(getApplicationContext(), "Deliverd", Toast.LENGTH_LONG).show(); } }, new IntentFilter(DELIVERED)); /*Send SMS*/ SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNo, null, msg, sentPI, deliverPI); } catch (Exception ex) { Toast.makeText(getApplicationContext(), ex.getMessage().toString(), Toast.LENGTH_LONG) .show(); ex.printStackTrace(); } } }); } @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; } }
0 Comment(s)