我通过我的应用程序成功发送了一条短信,但是当我使用双SIM卡手机时,我的应用程序无法运行,但是当我关闭其他SIM卡后,该消息将成功发送,我将向您显示我的代码,请帮助我。我的代码可用于双SIM卡手机的方式。
我已经尝试过Maher Abuthraa方法How to send a SMS using SMSmanager in Dual SIM mobile?
但是我不完全了解如何在我的编码中实现他的方法。
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();
}
}
我希望该应用即使在选定SIM卡中的双SIM卡手机中也能发送消息,只非常感谢您。