gpt4 book ai didi

java - SmsManager 在发送每条短信之间有延迟

转载 作者:行者123 更新时间:2023-12-01 22:43:32 26 4
gpt4 key购买 nike

早上好,感谢并抱歉我的英语!

我正在开发一个 Android 应用程序,我希望该应用程序使用 SmsManager 发送大量短信。这很好,但是当我尝试在每条短信的发送之间实现任何类型时,问题就出现了。我也尝试在 for、asynctask 中使用线程,但应用程序也进入休眠状态,并且我需要应用程序在发送短信时不会阻塞。

我读取外部(应用程序目录内)文件的编号,然后将这些数据放入哈希表中,然后执行 for 循环来调用 SmsManager 函数 sendTextMessage。

MainActivity.java

package com.example.howtosendsmsinandroid;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.AsyncTask;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{
Button btn;
//EditText et1,et2;
private static final int Request_Read_Contacts=0;
private static final int Request_Code_permission=2;

BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
public void onResume() {
super.onResume();
smsSentReceiver=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 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;
default:
break;
}

}
};
smsDeliveredReceiver=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 Delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
}

public void onPause() {
super.onPause();
unregisterReceiver(smsSentReceiver);
unregisterReceiver(smsDeliveredReceiver);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//et1=(EditText)findViewById(R.id.number);
//et2=(EditText)findViewById(R.id.Content);
String cPermission= Manifest.permission.SEND_SMS;
try{
if (ActivityCompat.checkSelfPermission(this,cPermission)!= PackageManager.PERMISSION_GRANTED){

ActivityCompat.requestPermissions(this,new String[]{cPermission},Request_Code_permission);
}

}
catch(Exception e){
e.printStackTrace();

}
String sPermission= Manifest.permission.INTERNET;
try{
if (ActivityCompat.checkSelfPermission(this,sPermission)!= PackageManager.PERMISSION_GRANTED){

ActivityCompat.requestPermissions(this,new String[]{cPermission},Request_Code_permission);

}
}
catch(Exception e){
e.printStackTrace();

}
}

public void onmessage(View v){
//String phonenumber = et1.getText().toString();
//String msg = et2.getText().toString();
try
{

Hashtable<Integer, ArrayList<String>> contacts = new Hashtable<Integer, ArrayList<String>>();

String linea;
InputStream fraw =
getResources().openRawResource(R.raw.numbers);

BufferedReader brin =
new BufferedReader(new InputStreamReader(fraw));
int count = 0;
while ((linea = brin.readLine()) != null){
//NO ENVIA SI SON 160 O MAS CARACTERES (DE FORMA ORDINARIA)

String[] parts = linea.split(";");
contacts.get(count);
ArrayList<String> numbers = new ArrayList<String>();
numbers.add(parts[0]);
numbers.add(parts[1]);
numbers.add(parts[2]);
contacts.put(count, numbers);
count++;



}

int i = 0;
for (Map.Entry<Integer, ArrayList<String>> entry: contacts.entrySet()){
SmsManager sms = SmsManager.getDefault();


String number = entry.getValue().get(0);
String nom = entry.getValue().get(1);
String animal = entry.getValue().get(2);
String msg = "Hola {{NOMBRE}}, le informamos que su {{ANIMAL}} se ha comido a su vecina. Contacte con nosotros a traves de www.porncharacters.com";
msg = msg.replace("{{NOMBRE}}", nom);
msg = msg.replace("{{ANIMAL}}", animal);
PendingIntent piSent=PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
PendingIntent piDelivered=PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
/*
Toast.makeText(getApplicationContext(), "Numero:" + number,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Message: " + msg,
Toast.LENGTH_LONG).show();


Toast.makeText(getApplicationContext(), "Numero: " + number,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Numero: " + nom,
Toast.LENGTH_LONG).show();

*/
sms.sendTextMessage(number, null, msg, piSent, piDelivered);
i++;




//Thread.sleep(60000);

}
fraw.close();
}
catch (Exception ex)
{
Log.e("Ficheros", "Error al leer fichero desde recurso raw");
}



}

}

最佳答案

最后,我成功解决了这个问题。我使用了 Handler 和 Thread 以及进度条。

我把代码放在这里,希望对你有帮助。

MainActivity.java

package com.example.howtosendsmsinandroid;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.os.Handler;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.Map;
import android.content.BroadcastReceiver;
import android.content.Context;

public class MainActivity extends Activity {
/*
EditText txtPhoneNo;

EditText txttime;

*/
//AsyncTask<?, ?, ?> runningTask;
EditText txtMessage;
Button btnSend;
Button btnLoad;
String p = Manifest.permission.SEND_SMS;
ProgressDialog barraProgreso;
private Hashtable<Integer, ArrayList<String>> contacts = new Hashtable<Integer, ArrayList<String>>();
int i = 0;
private Handler handler;
BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
public void onResume() {
super.onResume();
smsSentReceiver=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 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;
default:
break;
}

}
};
smsDeliveredReceiver=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 Delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));
}

public void onPause() {
super.onPause();
unregisterReceiver(smsSentReceiver);
unregisterReceiver(smsDeliveredReceiver);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//txtPhoneNo = (EditText) this.findViewById(R.id.nrotelf);
//txtMessage = (EditText) this.findViewById(R.id.editText2);
//txttime = (EditText) this.findViewById(R.id.editText);
txtMessage = this.findViewById(R.id.txtMessage);
btnSend = this.findViewById(R.id.btn);
btnLoad = this.findViewById(R.id.btnLoad);
btnLoad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
String linea;
InputStream fraw =
getResources().openRawResource(R.raw.numbers);

BufferedReader brin =
new BufferedReader(new InputStreamReader(fraw));
int count = 0;
while ((linea = brin.readLine()) != null) {
//NO ENVIA SI SON 160 O MAS CARACTERES (DE FORMA ORDINARIA)

String[] parts = linea.split(";");
contacts.get(count);
ArrayList<String> numbers = new ArrayList<String>();
numbers.add(parts[0]);
//numbers.add(parts[1]);
//numbers.add(parts[2]);
contacts.put(count, numbers);
count++;


}
Toast.makeText(getBaseContext(), "Numeros importados: " + contacts.size(), Toast.LENGTH_LONG).show();
}catch (Exception ex)
{
Log.e("Ficheros", "Error al leer fichero desde recurso raw");
}
}
});
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Obtener numero de telefono y mensaje
checkSMSStatePermission();

try {


barraProgreso = new ProgressDialog(MainActivity.this);
barraProgreso.setTitle("Buscando...");
barraProgreso.setMessage("Progreso...");
barraProgreso.setProgressStyle(barraProgreso.STYLE_HORIZONTAL);
barraProgreso.setProgress(0);
barraProgreso.setMax(contacts.size());
barraProgreso.show();

handler = new Handler();

new Thread(new Runnable() {
@Override
public void run() {
try {
while (barraProgreso.getProgress() <= barraProgreso.getMax()) {
Thread.sleep(10000);
handler.post(new Runnable() {
@Override
public void run() {
sendMessage();
barraProgreso.incrementProgressBy(1);
i++;
}
});

if(barraProgreso.getProgress() == barraProgreso.getMax()){
barraProgreso.dismiss();
}
}
}catch (InterruptedException er){
er.printStackTrace();
}
}
}).start();


// If phone number & message is not empty

} catch (Exception e) {
e.printStackTrace();
}

}
});
}

// Function send message sms
private void sendMessage() {
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
//String message = "Mensaje de prueba: " + i;

//String message = "Hola "+ contacts.get(i).get(1)+" , le informamos que su "+ contacts.get(i).get(2)+" se ha comido a su vecina. Contacte con nosotros a traves de www.porncharacters.com";
String message = txtMessage.getText().toString();

//el primer parametro recibe el contexto asi que lo
// llamas desde un fragment tendras
//cambiar el this por getContext()
sentPI = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(SENT), 0);
smsManager.sendTextMessage(contacts.get(i).get(0), null, message, sentPI, null);
//Toast.makeText(getBaseContext(), message.length(), Toast.LENGTH_LONG).show();

}

private void checkSMSStatePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.SEND_SMS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("Mensaje", "No se tiene permiso para enviar SMS.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 225);
} else {
Log.i("Mensaje", "Se tiene permiso para enviar SMS!");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
}

关于java - SmsManager 在发送每条短信之间有延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58481777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com