sending more than 160 charactors in sms - android

I just found out my app doesn't capable of sending more than 160 characters at a time. it works fine on characters less than 160. when I try to send more than 160 characters at a time. it display "messages sent" toast but message doesn't going anywhere what should I change to send more than 160 characters.
thank you
here is the code
public class MainActivity7 extends ActionBarActivity {
String value ;
Button button;
TextView editext2;
TextView editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity7);
editText = (TextView) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
editext2 = (TextView) findViewById(R.id.editText2);
Intent a = getIntent();
editText.setText(a.getStringExtra("item") );
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMSMessage();
}
});
}
private void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneno = editext2.getText().toString();
String message = editText.getText().toString();
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneno,null,message,null,null);
Toast.makeText(getApplicationContext(),"sms sent.",
Toast.LENGTH_LONG).show(); }
catch (Exception e){
Toast.makeText(getApplicationContext(),
"sms failed,please try again",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}

To send more than 160 characters in SMS you need to send it as a multiple SMS.
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(LONG_TEXT);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(getContext(), 0, mSendIntent, 0));
deliveryIntents.add(PendingIntent.getBroadcast(getContext(), 0, mDeliveryIntent, 0));
}
sm.sendMultiPartTextMessage(mDestAddr,null, parts, sentIntents, deliveryIntents);

Related

One Plus Device Issue while sending the SMS

One Plus device Issue getting the Exception when trying the Sending SMS in grammatically getting the Exception Java.lang.Security Exception .
try {
String message=selectedContactsMap.get(mobileNumber);
/* SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNumber,null, message,
null, null);*/
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(message);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(context, 0, new Intent(SMS_SENT), 0));
deliveryIntents.add(PendingIntent.getBroadcast(context, 0, new Intent(SMS_DELIVERED), 0));
}
sm.sendMultipartTextMessage(mobileNumber,null, parts, sentIntents, deliveryIntents);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(Birthday.this,"Send Message Successfully",Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}, 1000);
} catch (Exception e) {
// Toast.makeText(Birthday.this,"Something went
// wrong.",Toast.LENGTH_SHORT).show();
Toast.makeText(Birthday.this,"Error:-
"+e.toString(),Toast.LENGTH_SHORT).show();
e.printStackTrace();`enter code here`
progressDialog.dismiss();
}
All device is working the fine Only Problem is One Plus Device

Why my automatic reply doesn't send sms?

It says null to my message, and i want to take message that i write from another class, is it possible ? Can anyone help to see what's my error ? I've tried moving button to this class too, but it wont work either,
public class ReceiveMessage extends BroadcastReceiver {
private Button btnchange;
EditText qwer;
private Context ctx;
EditText text;
TextView asd;
static TextView messageBox;
static String phoneNumber;
private EditText pesan;
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
break;
}
for (SmsMessage msg : sms)
{
String msga = msg.getMessageBody();
String phoneNumber1 = msg.getOriginatingAddress();
String name = getContactName(context, phoneNumber1);
if (name != null && !name.equals("null"))
{
SmsManager smsManager = SmsManager.getDefault();
String sendTo = phoneNumber1;
// i want to locate my message that i write from another class
asd = (TextView) findViewById (R.id.textView1);
qwer = (EditText) findViewById (R.id.editText1);
try {
// here's the message that i want to send but the program says null inside my msg
String smsMessage = "Hi " +name+ "\n\n" +qwer.getText().toString()+ "\n\nThx";
smsManager.sendTextMessage(sendTo, null, smsMessage, null,
null);
}catch(Exception ex){
ex.printStackTrace();
System.out.println("errror karena " +ex.getMessage());
}
}
}
}
private EditText findViewById (int e) {
return null;
}
}
You can not access the another class inflated view from this class.
You need to check if you've declare this receiver in the manifest.
Have a look on this stack overflow question.
Android - SMS Broadcast receiver

Android Send SMS more than 160 characters

Hi I am trying to send sms to different mobile numbers with different text body, my code is also sending sms more than 160 characters but it sends to only one number not to all , Now I want to send multiparttext message to all contact list.pls help
private void sendSMS()
{
if (list_phone.size()!=0){
//new AddNewCategory().execute();
for (i = 0; i < len; i++){
SmsManager sm = SmsManager.getDefault();
ArrayList<String> message_parts = sm.divideMessage(list_MESSAGE_BODY.get(i));
// Log.e("Message parts 3", message_parts.get(3));
// Log.e("Message parts 4", message_parts.get(4));
Intent iSent = new Intent();
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
Intent iDel = new Intent();
PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);
if (message_parts.size() == 1)
{
String msg = message_parts.get(0);
sm.sendTextMessage(list_phone.get(i), null, msg, piSent, piDel);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
else
{
ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();
int ct = message_parts.size();
for (int i = 0; i < ct; i++)
{
sentPis.add(i, piSent);
delPis.add(i, piDel);
}
//Log.e("Message Parts", message_parts+"");
Log.e("Phone list", list_phone.get(i));
Log.e("Message parts"+i, message_parts.get(i));
sm.sendMultipartTextMessage(list_phone.get(i), null, message_parts, sentPis, delPis);
Toast.makeText(getApplicationContext(), " Message sent", Toast.LENGTH_SHORT).show();
}
}
else
{
// Toast.makeText(getApplicationContext(), "Phone list is empty.. Message not sent", Toast.LENGTH_SHORT).show();
}
}

Lock SMS in android

i developing an application where i want to block SMS of some specific numbers.for this purpose i have one an Activity and Second is BroadcastReceiver class. in Activity i have a list where User will enter a number that he want to block. but i don't how will List number will be Access in BroadcastReceiver class to block it. i Access An ArrayAdapter in BroadcastReceiver class but it does not perform to block a call. Any one help me..thanks in advance..
SmsLock.java
public class SmsLock extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
String phoneNumber;
String senderNum;
NumberListActivity ma = new NumberListActivity();
NumberListActivity num = new NumberListActivity();
String[] number = new String[] { "+923327765798", "+923219750751",
"+923445508726" };
#Override
public void onReceive(Context context, Intent intent) {
ArrayAdapter<String> adapter = ma.getArrayAdapter();
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
phoneNumber = currentMessage.getDisplayOriginatingAddress();
senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
}
for (int i = 0; i < adapter.getCount(); i++) {
if (senderNum.contains(adapter.getItem(i))) {
abortBroadcast();
}
}
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
NumberListActivity.java
public class NumberListActivity extends Activity {
SharedPreferences preferences1;
SharedPreferences.Editor spEditor1;
int count1 = 0;
ListView numList1;
Button btnAdd1;
ArrayList<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1;
public static final String Place1 = "placeKey";
SmsLock brd = new SmsLock();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_list);
preferences1 = getSharedPreferences("Place1", Context.MODE_PRIVATE);
spEditor1 = preferences1.edit();
count1 = preferences1.getInt("count1", 0);
if (count1 > 0) {
for (int i = 0; i < count1; i++) {
list1.add(preferences1.getString("Value1[" + i + "]", ""));
}
}
final EditText edit = (EditText) findViewById(R.id.Item);
numList1 = (ListView) findViewById(R.id.Smslist);
btnAdd1 = (Button) findViewById(R.id.Add);
adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list1);
numList1.setAdapter(adapter1);
numList1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
/*
* list.remove(position); //preferences.edit().clear().commit();
* preferences.edit().remove("Value["+position+"]").commit();
* //count-=1; // adapter.remove(adapter.getItem(position));
*/
count1 = preferences1.getInt("count1", 0);
// if (count > 0) {
for (int i = position; i < count1; i++) {
// list.add();
if (i < count1)
spEditor1.putString(
"Value1[" + i + "]",
preferences1.getString("Value1[" + (i + 1)
+ "]", ""));
spEditor1.commit();
}
// }
list1.remove(position);
count1 -= 1;
spEditor1.putInt("count1", count1);
spEditor1.commit();
// preferences.edit().remove(position);
adapter1.notifyDataSetChanged();
}
});
btnAdd1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// EditText edit = (EditText) findViewById(R.id.txtItem);
spEditor1.putString("Value1[" + count1 + "]", edit.getText()
.toString());
spEditor1.commit();
list1.add(preferences1.getString("Value1[" + count1 + "]", ""));
count1 += 1;
spEditor1.putInt("count1", count1);
spEditor1.commit();
adapter1.notifyDataSetChanged();
}
});
}
public ArrayAdapter<String> getArrayAdapter() {
return adapter1;
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent int1 = new Intent(NumberListActivity.this, Main.class);
startActivity(int1);
finish();
}
}
In your broadCast receiver, I assume you have successfully extracted the incoming SMS's phone number and the list of Numbers that are blocked. Then this code is iterating through the list of blocked numbers and entering the if-block, appropriately.
for (int i = 0; i < adapter.getCount(); i++) {
if (senderNum.contains(adapter.getItem(i))) {
abortBroadcast();
}
}
If you are sure above flow is working fine and still you are not able to block the SMS being received, then one following might be the cause:
Priotity: In your manifest file you set a priority for for your application to receive the SMS_RECEIVED intent. Suppose, that your app has a priority 10 and the OS's default SMS app has priority 20, then your app is getting a chance to respond to SMS_RECEIVED intent only after the defaul SMS app. This explains why the abortBroadcast() does not seem to work in above code.
I suggest you set the highest priority possible for your broadCastReceiver in the manifest, so that you get the first chance to handle SMS_RECEIVED intent.
From API 19 and above, there can be only one SMS application on a device. If there are 2 or more apps that have a permission to intercept SMS then user has to specify which one of the availaible applications should serve as the defult SMS app. Then the chosen default SMS app will only have the permission to respond to SMS_RECEIVED intent and other applications will not get a chance to receive SMS_RECEIVED intent.
So you also need to set your target SDK to 18 or below to use the SMS interception normally or you need to read some documentation if you want your app to be compatible with API 19.
In order to access the numbers from which sms you want to block first you need to store all the numbers either in your SQlite database or in preferences so that they can be accessed in broadcast receivers then use
abortBroadcast();
to prevent messages to go into inbox.

How to bring contacts up to send a text

I have an application that defines a word and then sends the definition via SMS. The problem is that the user has to type in a phone number. I was thinking that perhaps the app should bring up the contacts list, so the user can select a contact which the user can then send the message to that contact. Here is my code for the SMSActivity:
public class SMSActivity extends Activity
{
String APPTAG = "SMSTransmit";
//Private static strings:
private final static String SENT = "SMS_SENT";
private final static String DELIVERED = "SMS_DELIVERED";
private Button btnSend;
private Button btnExit;
private Button btnClear;
private EditText etPhoneNumber;
private EditText etUserMessage;
//Private BroadcastReceiver member variables:
private SMSDispatchReceiver sendReceiver = null;
private SMSReceiptReceiver receiptReceiver = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
Bundle extras = getIntent().getExtras();
String definiedWord = null;
if (extras != null)
{
definiedWord = extras.getString("DEFINITION");
}
else
{
definiedWord = "None";
}
Log.d("recievedAGAIN", definiedWord);
Log.v(APPTAG, "MainActivity: onCreate() called");
//Create a new broadcast receiver for sending the SMS
sendReceiver = new SMSDispatchReceiver();
//Create a new broadcast receiver for receipt of the send SMS
receiptReceiver = new SMSReceiptReceiver();
//Register the new receivers (as new IntentFiler() objects):
registerReceiver(sendReceiver, new IntentFilter(SENT));
registerReceiver(receiptReceiver, new IntentFilter(DELIVERED));
//Get the view objects:
btnSend = (Button) findViewById(R.id.btnSend);
btnClear = (Button) findViewById(R.id.btnClear);
btnExit = (Button) findViewById(R.id.btnExit);
etPhoneNumber = (EditText) findViewById(R.id.etNumber);
etUserMessage = (EditText) findViewById(R.id.etMessage);
//Disable the soft keyboard (this is only in general):
InputMethodManager inMethodMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inMethodMgr.hideSoftInputFromInputMethod(etPhoneNumber.getWindowToken(), 0);
inMethodMgr.hideSoftInputFromInputMethod(etUserMessage.getWindowToken(), 0);
//Set the hint for the EditText boxes:
etPhoneNumber.setHint("Enter phone number (Default = 5556)");
etUserMessage.setHint(definiedWord);
//Create an click event listener for the Send button (OnClickListener):
btnSend.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String strMessage = null;
String strNumber = null;
//Get the phone number from the EditText box:
strNumber = etPhoneNumber.getText().toString();
//Check the phone number:
if (strNumber.length() <= 0)
{
//No phone number, then get the default (5556):
strNumber = SMSProperties.getPhoneNumber();
}
//Get the message from the EditText box:
strMessage = etUserMessage.getText().toString();
//Check the message contains some content:
if (strMessage.length() > 0)
{
//Sent the SMS to the phone number
sendSMS(strNumber, strMessage);
} else
{
//Warn the user and reset the focus / hint:
Toast.makeText(getBaseContext(), "No message text! Please enter some text for the SMS!!", Toast.LENGTH_SHORT).show();
etUserMessage.setHint("Enter message text here . . . ");
etUserMessage.requestFocus();
}
}
});
//Create an click event listener for the Clear button (OnClickListener):
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear the appropriate EditText box:
if (etUserMessage.isFocused()) {
//Clear the text in the user message EditText box:
etUserMessage.setText("");
//Set the hint in the user message EditText box:
etUserMessage.setHint("Enter message text here . . . ");
} else if (etPhoneNumber.isFocused()) {
//Clear the text in the phone number EditText box:
etPhoneNumber.setText("");
//Set the hint in the phone number EditText box:
etPhoneNumber.setHint("Enter phone number (Default = 5556)");
}
}
});
//Create an click event listener for the Exit button (OnClickListener):
btnExit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Finish the activity:
//NOTE: As this is the main activity it will cause onDestroy() to be called!
finish();
}
});
}
//Method to send an SMS message to another device:
private void sendSMS(String strNum, String strMsg)
{
//TODO: Create a pending intent for the SMSDistatchReceiver (SENT):
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SENT"), 0);
//TODO: Create a pending intent for the SMSReceiptReceiver (DELIVERED):
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent("DELIVERED"), 0);
//Get a default SmsManager object:
SmsManager smsMgr = SmsManager.getDefault();
//TODO: Send the SMS using the SmsManager:
smsMgr.sendTextMessage(strNum, null, strMsg, sentPI, deliveredPI);
}
consider contact is a button to bring up the contact list
contact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View g) {
Intent q = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(q, 1001);
}
});
and to handle the result (which is referenced by 1001) use this:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
// getting the URI from result for further working
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
//this string will hold the contact number
String cNumber = phones.getString(phones.getColumnIndex("data1"));
//this string will hold the contact name
String cName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
}
}}
}

Categories

Resources