• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps not sending sms

M0TRIX

Lurker
hello.

could you find the problem? it dosen't send an sms :


import java.util.List;



import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DatabaseActivity extends Activity {

private final String TAG = "DatabaseActivity";

private DatabaseHandler dbHandler;
private Contact contact;
private List<Contact> list;
private Button btnAdd;
private EditText etName;
private EditText etPhone;
private ListView listView;
private ListAdapter adapter;

@override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Log.i(TAG, "Inside onCreate()");


dbHandler = new DatabaseHandler(this);
contact = new Contact();
adapter = new ListAdapter(this);


etName = (EditText)findViewById(R.id.etName);
etPhone = (EditText)findViewById(R.id.etPhone);


listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(new OnItemClickListener() {
@override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.i(TAG, "item " + position + " clicked.");

TextView tv = (TextView) v.findViewById(R.id.tvId);
Log.i(TAG, ">> " + tv.getText());

/***************************************************************
* One of these methods should be uncommented at the same time *
***************************************************************/
dbHandler.deleteContact(Long.parseLong(tv.getText().toString()));
//dbHandler.updateContact(Long.parseLong(tv.getText().toString()));
refreshList();

}
});


btnAdd = (Button) findViewById(R.id.btnAddToDatabase);
btnAdd.setOnClickListener(new OnClickListener() {
@override
public void onClick(View v) {

Log.i(TAG, "Button clicked.");

boolean validate1 = false;
boolean validate2 = false;

String name = etName.getText().toString();
if(name.equalsIgnoreCase(""))
Toast.makeText(DatabaseActivity.this, "Pease enter a name.", Toast.LENGTH_LONG).show();
else {
contact.setName(name);
validate1 = true;
}

String phone = etPhone.getText().toString();
if(phone.equalsIgnoreCase(""))
Toast.makeText(DatabaseActivity.this, "Pease enter phone number.", Toast.LENGTH_LONG).show();
else {
contact.setPhoneNumber(phone);
validate2 = true;
}

if(validate1 && validate2) {
//sendSmsBySIntent();
sendSmsByManager();
// sendSmsByVIntent();
etName.setText("");
etPhone.setText("");

dbHandler.insertContact(contact);
refreshList();


}
}
});
}

public void sendSmsByManager() {
try {
// Get the default instance of the SmsManager
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(etPhone.getText().toString(),
null,
"sent",
null,
null);
Toast.makeText(getApplicationContext(), "Your sms has successfully sent!",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),etPhone.getText().toString()+"Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
public void sendSmsByVIntent() {

Intent smsVIntent = new Intent(Intent.ACTION_VIEW);
// prompts only sms-mms clients
smsVIntent.setType("vnd.android-dir/mms-sms");

// extra fields for number and message respectively
smsVIntent.putExtra("address", etPhone.getText().toString());
smsVIntent.putExtra("sms_body", "sent...");
try{
startActivity(smsVIntent);
} catch (Exception ex) {
Toast.makeText(DatabaseActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}

}
public void sendSmsBySIntent() {
// add the phone number in the data
Uri uri = Uri.parse("smsto:" + etPhone.getText().toString());

Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
// add the message at the sms_body extra field
smsSIntent.putExtra("sms_body", "sent...!");
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(DatabaseActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}

@override
protected void onResume() {
super.onResume();

dbHandler.open();
// dbHandler.clearTable();
refreshList();
}

@override
protected void onPause() {
super.onPause();

dbHandler.close();
}

protected void refreshList() {
list = dbHandler.getAllContacts();
adapter.setData(list);
listView.setAdapter(adapter);
}
}

there are 3 sending sms method.but 2 of them works!!!

i need sendSmsByManager() method to work.

tnx
 
Back
Top Bottom