I successfully send a SMS through my app, but when I used dual sim mobile phones my app does not function but when I turn off other sim the message will sent successfully, I will show you my code please help me what is the way for my codes to work for dual sim phones.
I already tried Maher Abuthraa method How to send a SMS using SMSmanager in Dual SIM mobile?
But i dont fully understand how i can implement his method it to my codings.
Timeinsms.java
package com.example.serviceapplication;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.app.PendingIntent;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class Timeinsms extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 1;
DatabaseHelper myDb;
EditText editTextId, txtphoneNo;
TextView editTextsmsi, txtMessage;
Button btngetData, btnView , sendBtn;
String phoneNo,message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeinsms);
myDb = new DatabaseHelper(this);
editTextId = (EditText) findViewById(R.id.editText_idin);
editTextsmsi = (TextView) findViewById(R.id.editText_smsin);
btngetData = (Button) findViewById(R.id.button_view);
btnView = (Button) findViewById(R.id.button_viewALL);
sendBtn = (Button) findViewById(R.id.button_send);
txtphoneNo = (EditText) findViewById(R.id.editTextno);
txtMessage = (TextView) findViewById(R.id.editText_smsini);
getData();
viewAll();
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
}
//////////////////SMS CODE////////////////////////
protected void sendSMSMessage() {
phoneNo = txtphoneNo.getText().toString();
message = txtMessage.getText().toString();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
PendingIntent pi = PendingIntent.getActivity(this, 0,
new Intent(this, Timeinsms.class), 0);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String
permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09065723958", null, message,
null, null);
Toast.makeText(getApplicationContext(), "Notice:" +
"TIME IN SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"TIME IN failed, please try again.",
Toast.LENGTH_LONG).show();
return;
}
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09988456526", null, message,
null, null);
Toast.makeText(getApplicationContext(), "TIME IN SMS
sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"TIME IN failed, please try again.",
Toast.LENGTH_LONG).show();
return;
}
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("09395393329", null, message,
null, null);
Toast.makeText(getApplicationContext(), "TIME IN SMS
sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"TIME IN failed, please try again.",
Toast.LENGTH_LONG).show();
return;
}
}
}
}
//////////////////SMS CODE END//////////////////////
public void getData() {
btngetData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = editTextId.getText().toString();
if (id.equals(String.valueOf(""))) {
editTextId.setError("Enter id to get data");
return;
}
Cursor res = myDb.getData(id);
String data = null;
if (res.moveToFirst()) {
data =
"Id:" + res.getString(0) + "\n\n" +
"Time In :" + res.getString(1) + "\n" +
"Customer :" + res.getString(2) + "\n" +
"Branch :" + res.getString(3) + "\n" +
"Machine :" + res.getString(4) + "\n";
}
editTextsmsi.setText("TIME IN SMS" + "\n\n" + data);
txtMessage.setText("TIME IN SMS" + "\n\n" + data);
}
});
}
public void viewAll() {
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Job No :" + res.getString(0) + "\n\n");
buffer.append("Time :" + res.getString(1) + "\n\n");
buffer.append("Customer :" + res.getString(2) + "\n\n");
buffer.append("Branch :" + res.getString(3) + "\n\n");
buffer.append("Markss :" + res.getString(4) + "\n\n\n");
}
showMessage("Time In History", buffer.toString());
}
});
}
private void showMessage(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.create();
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
I expect the app to send a message even in dual sim phone in selected sim card only thank you so much.