我正在尝试使用SmsManager发送一些SMS消息,并在发送某些消息时显示进度。如何查看邮件是否真的已发送?
我有这个:
private class SomeAsyncTask extends
AsyncTask<ArrayList<Person>, Integer, Void> {
private ProgressDialog dialog;
private Context context;
public SomeAsyncTask(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
protected void onPreExecute() {
this.dialog.setMessage("Processing!");
this.dialog.show();
}
@Override
protected Void doInBackground(ArrayList<Person>... params) {
dialog.setMax(params[0].size());
for (int i = 0; i < params[0].size(); i++) {
SmsManager.getDefault().sendTextMessage(params[0].get(i).getNum(), null, "test", null, null);
publishProgress((int) ((i / (float) params[0].size()) * 100));
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void v) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
每次成功发送消息后,我想更新进度。你建议我做什么?谢谢。
答案 0 :(得分:1)
您需要实施广播接收器来处理msg&amp;的“发送报告”。信息的“错误报告”
有两种类型,一种是用于msg传递&amp;另一个是错误,如果发生任何错误(linke泛型错误等)。
答案 1 :(得分:0)
**Call this method where you want to send Sms**
private String SimState = "";
private String address=""; // Recipient Phone Number
private String message=""; // Message Body
private void sendSms()
{
if(isSimExists())
{
try
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1)
{
int resultCode = getResultCode();
switch (resultCode)
{
case Activity.RESULT_OK: Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(SENT));
SmsManager smsMgr = SmsManager.getDefault();
smsMgr.sendTextMessage(address, null, message, sentPI, null);
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage()+"!\n"+"Failed to send SMS", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
else
{
Toast.makeText(this, SimState+ " " + "Cannot send SMS", Toast.LENGTH_LONG).show();
}
}
// For receiving sms
class SMSReceiver extends BroadcastReceiver
{
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
// Sms Received Your code here
}
}
}
**Note: You have to specify android.permission.SEND_SMS and
android.permission.RECEIVE_SMS permissions in manifest file and
also the receiver**
<receiver android:name=".SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tekeli.order"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.SEND_SMS" ></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityOrderActivity"
android:label="@string/app_name">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".B" ></activity>
<activity android:name=".C"></activity>
</application>
</manifest>
**sim exist or not method**
public boolean isSimExists() {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int SIM_STATE = telephonyManager.getSimState();
if (SIM_STATE == TelephonyManager.SIM_STATE_READY)
return true;
else {
switch (SIM_STATE) {
case TelephonyManager.SIM_STATE_ABSENT: // SimState =
// "No Sim Found!";
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED: // SimState =
// "Network Locked!";
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED: // SimState =
// "PIN Required to access SIM!";
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED: // SimState =
// "PUK Required to access SIM!";
// // Personal
// Unblocking Code
break;
case TelephonyManager.SIM_STATE_UNKNOWN: // SimState =
// "Unknown SIM State!";
break;
}
return false;
}
}