I made sms application, it's running well if the message length < 160, but, when message length is more than 160, the emulator said that the application force close,
How I fix that?
here is my code..
private void kirimSMS(String string1, String string2) {
pi1 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
pi2 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"),
0);
sendBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"SMS terkirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Gagal Kirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"No Services", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(KirimSMS.this.getBaseContext(), "No PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(KirimSMS.this.getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
}
}};
deliveryBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan terkirim", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan Tidak terkirim", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
//SmsManager.getDefault().sendTextMessage(string1, null, string2, pi1,pi2);
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(stringIsiPesan);
smsManager.sendMultipartTextMessage(stringNoHp, null, parts, null, null);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnEnkripsi:
if (cekField()) {
String kunci = null;
String hasil = null;
if (MainActivity.DEFAULT_KEY.equals(""))
kunci = "ABCD";
else
kunci = MainActivity.DEFAULT_KEY;
Kriptoku enktripsi = new Kriptoku();
hasil = enktripsi.enkripsibaru(txtPesan.getText().toString(),kunci);
txtPesan.setText(hasil);
btnEnkripsi.setEnabled(false);
} else {
AlertDialog.Builder loBuilder2 = new AlertDialog.Builder(this);loBuilder2
.setMessage("lengkapi data..")
.setTitle("Oo..Oo..")
.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
loBuilder2.create();
loBuilder2.show();
}
break;
case R.id.btnKirimPesan:
//if (txtPesan.length() <= 160) {
if (cekField()) {
kirimSMS(this.txtNoHp.getText().toString(), this.txtPesan.getText().toString());
dao = SQLiteDAO.getInstance(this,
new Class[] { Outbox.class });
Outbox localSMS = new Outbox();
localSMS.setNoHp(this.txtNoHp.getText().toString());
localSMS.setPesan(this.txtPesan.getText().toString());
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("HH:mm, dd MMM yyyy");
Calendar localCalendar = Calendar.getInstance();
localSMS.setTime(localSimpleDateFormat.format(localCalendar.getTime()));
dao.insert(localSMS);
finish();
} else {
AlertDialog.Builder loBuilder1 = new AlertDialog.Builder(this);
loBuilder1
.setMessage("Lengkapi Data")
.setTitle("Aduuhh...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick{ DialogInterface dialog,int which) {
//TODO Auto-generated method stub
}
});
loBuilder1.create();
loBuilder1.show();
}
} //else {
// Toast.makeText(this, String.valueOf("Karakter lebih dari 160"), Toast.LENGTH_LONG).show();
//return;
// break;
}
please, help me
SMS = Short Message Service, where 'short' is 160 characters or less. If you want to send more, break the message into two, or more, parts.
by default the max length of an SMS can be only 160 characters
that what SHORT means in SMS = "SHORT MESSAGING SERVICE"
if you still want to send more than 160 characters , you need to split it into two or convert it into MMS
so use an if condition to check if the length is greater than 160 characters
Related
I am a newcomer to androi. My problem is the following I have a code that sends text messages all right but I would like to be able to do that when the sending of the message fails automatically try to send it again about 3 times after that if it is not sent since it does nothing.
Greetings I hope you can help me I had a good time with this problem.
Here I leave the code.
public class Principal extends Activity {
EditText txtPhone;
EditText txtMsg;
Button btnSend;
TextView msj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
txtPhone = ((EditText)findViewById(R.id.txtPhone ));
txtMsg = ((EditText)findViewById(R.id.txtMsg ));
btnSend = ((Button)findViewById(R.id.btnSend ));
msj = ((TextView)findViewById(R.id.txtmsj));
btnSend.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
PendingIntent sentIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_SENT"), 0);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
msj.setText("SMS SENT");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
msj.setText("I can not send SMS");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
msj.setText("service not available");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
msj.setText("PDU (Protocol Data Unit) es NULL");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
msj.setText("turn on the radio");
break;
}
}
}, new IntentFilter("SMS_SENT"));
SmsManager sms = SmsManager.getDefault();
if( txtPhone.getText().toString().length()> 0 &&
txtMsg.getText().toString().length()>0 )
{
sms.sendTextMessage( txtPhone.getText().toString() , null, txtMsg.getText().toString() , sentIntent, null);
}
else
{
Toast.makeText(getApplicationContext(), "Can not send, data is incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
I am trying to develop sms app that can load messages and tel numbers from server and store it internally, therefore when trying to send all messages at once using loop only some of them are sent, and this is my code. please help
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
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(), "Delivered",
Toast.LENGTH_LONG).show();
}
}, new IntentFilter(DELIVERED));
/*Send SMS*/
ArrayList<Messages> messagesArrayList = new ArrayList<>();
/* loading messages and phone into arraylist */
messagesArrayList = db.getMessages();
for (int i = 0; i < messagesArrayList.size() ; i++) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("090" + messagesArrayList.get(i).getPhone(), null, messagesArrayList.get(i).getBody(), sentPI,
deliverPI);
}
}catch(Exception ex){
Toast.makeText(getApplicationContext(),
ex.getMessage().toString(), Toast.LENGTH_LONG)
.show();
ex.printStackTrace();
}
}
});
use this
String strnum="100864445556;1004522145586;100855822557;100885222559";
Uri smsToUri = Uri.parse("smsto:" + strnum);
if you are getting numbers from database query then
with the help of loops
make string strnum = num1 + ";" + num2 + ";" + num3 + ";" so on
This function will call another function (prince.Encrypt) that encrypts (PRINCE algorithm) the text message. But once it is linked with the encryption function, sending SMS fails. The function was working fine before encryption was included.
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v, String args)
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
{
LongBuffer messageBuf = TooLong.messageToLongBuffer(message);
messageBuf.flip();
long[] messageData = new long[messageBuf.remaining()];
LongBuffer i = messageBuf.get(messageData);
String v1=prince.Encrypt(i, k0, kop, k1, t);
sendSMS(phoneNo, v1);
}
else
Toast.makeText(getBaseContext(),"Please enter both phone number and message.",Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
}
//---sends a SMS message to another device---
private void sendSMS(String phoneNumber, String v1)
{
/*
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, test.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
*/
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, v1, sentPI, deliveredPI);
}
}
In my application when i pass an intent into another class,so that user can send sms messages and that data get stored in database,..and when user click on select contact button he get the contacts checked on which he send the messages,firstly it worked fine but when i added update code on button click it start giving back the error..and logcat is showing me the error
03-24 11:45:10.811: E/AndroidRuntime(18748): android.content.res.Resources$NotFoundException: String resource ID #0x3
03-24 11:45:10.811: E/AndroidRuntime(18748): at android.widget.Toast.makeText(Toast.java:311)
smsSend.java
public class SmsSend extends Activity implements OnClickListener {
BroadcastReceiver smsSentReciver, smsSentDelivery;
static EditText edName, edMessage;
static int ResultCode = 12;
static ArrayList<String> sendlist = new ArrayList<String>();
Button btnSaveForLater, btnStart, btnExternal, btnSelect;
static TextView txt;
static StringBuilder conct = new StringBuilder();
static String contacts = "";
String delim = ";";
public static String Name;
TextView ed;
int i = 0;
String rowid, rowid1, value;
int j;
String Name1, msg, msg1;
String[] cellArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.create_camp);
btnExternal.setOnClickListener(this);
txt = (TextView) findViewById(R.id.textnum2);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
rowid = getIntent().getStringExtra("rowid");
value = getIntent().getStringExtra("key");
rowid1 = getIntent().getStringExtra("rowid1");
entryData();
edName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edName.setError(null);
}
});
edMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edMessage.setError(null);
}
});
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setError(null);
}
});
// Toast.makeText(getApplication(), contacts.toString(),
// Toast.LENGTH_LONG).show();
Name1 = edName.getText().toString();
msg1 = edMessage.getText().toString();
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
edName.setText(null);
edMessage.setText(null);
conct.delete(0, conct.length());
contacts = null;
txt.setText("0");
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(smsSentReciver);
unregisterReceiver(smsSentDelivery);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
smsSentReciver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "sms has been sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Fail",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service",
Toast.LENGTH_SHORT).show();
default:
break;
}
}
};
smsSentDelivery = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sms Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Sms not Delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelp entry = new DatabaseHelp(SmsSend.this);
entry.open();
switch (v.getId()) {
case R.id.btnSelect:
Intent a = new Intent(SmsSend.this, MainActivity.class);
startActivityForResult(a,ResultCode);
break;
case R.id.btnExternal:
Intent file = new Intent(SmsSend.this, File_Selecter.class);
startActivity(file);
break;
case R.id.btnStart:
Log.i("SMS", "Sendlist Size: " + sendlist.size());
if (edName.getText().toString().length() == 0
|| edName.getText().toString().length() == 0
|| txt.getText().equals("0")) {
edName.setError("First name is required!");
edMessage.setError("Message is required!");
txt.setError("Contacts required!");
} else {
/* if (rowid1 != null
&& edName.getText().toString().length() != 0
&& edName.getText().toString().length() != 0
&& txt.getText().equals("0")
&& edName.getText().toString() == value) {
Toast.makeText(getApplication(), "update", Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this,
0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(
this, 0, new Intent("SMS_DELIVERED"), 0);
Log.i("SMS", "contacts: " + contacts);
contacts = conct.toString();
cellArray = contacts.split(";");
for (int a1 = 0; a1 < cellArray.length; a1++) {
smsManager.sendTextMessage(cellArray[i].toString(),
null, msg1, piSend, piDelivered);
}
long ltt = Long.parseLong(rowid1);
entry.updateEntry(ltt, Name1, msg1, contacts);
Toast.makeText(getApplication(), "updated",
Toast.LENGTH_LONG).show();
} else {*/
boolean diditwork1 = true;
try {
Toast.makeText(getApplication(), "insert", Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
PendingIntent piSend = PendingIntent.getBroadcast(this,
0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered = PendingIntent.getBroadcast(
this, 0, new Intent("SMS_DELIVERED"), 0);
Log.i("SMS", "contacts: " + contacts);
contacts = conct.toString();
//Toast.makeText(getApplication(), contacts.toString(), Toast.LENGTH_LONG).show();
cellArray = contacts.split(";");
Toast.makeText(getApplication(), cellArray.length, Toast.LENGTH_LONG).show();
for (int a1 = 0; a1 < cellArray.length; a1++) {
smsManager.sendTextMessage(cellArray[i].toString(),
null, msg1, piSend, piDelivered);
}
DatabaseHelp entry1 = new DatabaseHelp(SmsSend.this);
entry1.open();
entry1.entryCreate(Name1, msg1, contacts);
entry1.close();
}
catch (Exception e) {
diditwork1 = false;
String erroe = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(erroe);
d.setContentView(tv);
d.show();
} finally {
if (diditwork1) {
Dialog d = new Dialog(this);
d.setTitle("Heck Yeah!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
}
edName.setText("");
edMessage.setText("");
txt.setText("0");
break;
}
}
//}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ResultCode) {
if (resultCode == RESULT_OK) {
sendlist = data.getStringArrayListExtra("name");
Toast.makeText(getApplication(), sendlist.size(), Toast.LENGTH_LONG).show();
if (sendlist != null) {
for (int i = 0; i < sendlist.size(); i++) {
conct.append(sendlist.get(i).toString());
conct.append(delim);
//Toast.makeText(getApplication(), conct.toString(),Toast.LENGTH_LONG).show();
}
}
}
i = sendlist.size();
txt.setText(Integer.toString(i));
if (resultCode == RESULT_CANCELED) {
}
}
}
}
MainaActiivty.java
public class MainActivity extends Activity implements OnItemClickListener {
ArrayList<String> name1 = new ArrayList<String>();
static ArrayList<String> phno1 = new ArrayList<String>();
ArrayList<String> phno0 = new ArrayList<String>();
MyAdapter ma;
Button send;
String[] cellArray = null;
int[] str;
int v = 0;
String contacts;
static int check1;
ListView lv;
int index;
int top;
StringBuilder b = new StringBuilder();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.get);
// TextView txt = (TextView) findViewById(R.id.textView1);
getAllCallLogs(this.getContentResolver());
lv = (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// b = SmsSend.contacts;
contacts = SmsSend.contacts;
if (SmsSend.contacts != null) {
cellArray = contacts.split(";");
// Toast.makeText(getApplication(),contacts.toString(),
// Toast.LENGTH_LONG).show();
for (int i = 0; i < cellArray.length; i++) {
for (int j = 0; j < phno1.size(); j++) {
if (cellArray[i].equals(phno1.get(j))) {
Toast.makeText(getApplication(),cellArray[i].toString(),
Toast.LENGTH_LONG).show();
ma.setChecked(j, true);
}
}
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contact_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addPage:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
StringBuilder checkedcontacts = new StringBuilder();
System.out.println(".............." + ma.mCheckStates.size());
for (int i = 0; i < name1.size(); i++)
{
if (ma.mCheckStates.get(i) == true) {
phno0.add(phno1.get(i).toString());
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
} else {
System.out.println("..Not Checked......"
+ name1.get(i).toString());
}
}
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
Toast.makeText(getApplication(), phno0.size(), Toast.LENGTH_LONG).show();
finish();
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
ma.toggle(arg2);
}
public void getAllCallLogs(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
System.out.println(phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener {
public SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
MyAdapter() {
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// Save ListView state
#Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.row, null);
tv = (TextView) vi.findViewById(R.id.textView1);
tv1 = (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText(name1.get(position));
tv1.setText(phno1.get(position));
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
try this one:
Toast.makeText(getApplication(), sendlist.size().tostring(), Toast.LENGTH_LONG).show();
or else:
Toast.makeText(getApplication(), sendlist.size().toString()+"", Toast.LENGTH_LONG).show();
bcoz it will allow second params in a string.. and also use getApplication instead of this one example :
Toast.makeText(getApplicationContext(),
"Error during request: " + e.getLocalizedMessage(),
Toast.LENGTH_LONG).show();
Thank you,,
Toast.makeText(getApplication(), sendlist.size(), Toast.LENGTH_LONG).show();
sendlist.size() returns an int and passing an int to Toast.makeText() uses the overload that attempts to load a string resource with the given id.
Replace with e.g. Integer.toString(sendlist.size()) to toast the integer value.
You have the same issue in multiple places. Check all your Toast.makeText() calls.
The second param to Toast must be a String type.
Concatenate second param like this:
Toast.makeText(getApplication(), "" + sendlist.size(), Toast.LENGTH_LONG).show();
try this one
Toast.makeText(ExploreMatchDetail.this,sendlist.size()+"",Toast.LENGTH_SHORT).show();
My Android application is sending SMS and it's working fine. However, when the user enters a wrong number, the SMS is sent without error message. Therefore, the user doesn't know that he entered a wrong number. How can I fix that?
Activity.java
StringTokenizer st=new StringTokenizer(phoneNo,",");
while (st.hasMoreElements())
{
String tempMobileNumber = (String)st.nextElement();
if(tempMobileNumber.length()>0 && sms.trim().length()>0)
{
sendSMS(tempMobileNumber, sms);
}
else
{
Toast.makeText(getBaseContext(),
"Please enter both phone number and message.",
Toast.LENGTH_SHORT).show();
}
}
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
},new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
You Need to validate whether User Input is phone Number or not
For Example:Mobile No must be 10 digit
String number="2525252212"
Pattern mobileNo= Pattern.compile("\\d{10}");
Matcher matcher = mobileNo.matcher(number);
if (matcher.matches()) {
//go on
} else {
//Show Dialog
}
There's a library i use for that : libphonenumber https://code.google.com/p/libphonenumber/
There are some number verification methods in there. Unless you want to implement the verifications manually.