私はAndroidのSMS機能を使用しています。私は、自分のアプリケーション要件として限られた数の連絡先にsms経由でユーザーの住所と住所を送信できます。ユーザーが送信ボタンをクリックすると、進行状況ダイアログが表示され、すべての連絡先にSMSを送信した後、進行状況ダイアログが消えるはずです。
私はこれをたくさん探しましたが、それは私がアンドロイドの初心者なので、私のアプリケーションのためにそれを行う方法を私は非常に混乱させる!AsyncTaskの進捗状況ダイアログで複数のSMSを送信する
誰もが私のクラスで方法、ここで私は、SMSを送信するための私のJavaクラスを含めていますonPreExecute()、onPostExecute()とdoInBackground()を実装する方法で私を助けてくださいすることができます。
package com.example.ghaznavi.myapp;
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.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
import android.content.IntentFilter;
public class SmsHandler{
Settings setObj=new Settings();
double latitude,longitude;
public SmsHandler() {
}
public void SendSms(final Context hcontext)
{
GPSService mGPSService = new GPSService(hcontext);
LocationAddress locObj=new LocationAddress();
mGPSService.getLocation();
latitude = mGPSService.getLatitude();
longitude = mGPSService.getLongitude();
StringBuilder sb = new StringBuilder(160);
String addd= locObj.getAddressFromLocation(latitude,longitude,hcontext);
sb.append("Hi, I m in trouble, Please Help!\n\n");
if ((latitude != 0.0) && (longitude!= 0.0)) {
sb.append("Map Link:").append("\n").append("https://maps.google.com/maps?q=").append(latitude).append("%2C").append(longitude).append("\n\n");
}
if (addd != null) {
sb.append("Address: ").append(addd).append("\n\n");
}
sb.append("- My Application");
setObj.Initialize(hcontext);
if (setObj.GetContactListCount()!=0)
{
for(int i=0;i<setObj.GetContactListCount();i++)
{
try
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(hcontext, 0, new Intent(
SENT), 0);
BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(hcontext, "SMS sent Successfully",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(hcontext, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(hcontext, "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(hcontext, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(hcontext, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
};
SmsManager localSmsManager = SmsManager.getDefault();
if (sb.toString().length() <= 160) {
hcontext.registerReceiver(sendSMS, new IntentFilter(SENT));
localSmsManager.sendTextMessage(setObj.GetContactListNumber()[i], null, sb.toString(), sentPI, null);
} else {
hcontext.registerReceiver(sendSMS, new IntentFilter(SENT));
localSmsManager.sendTextMessage(setObj.GetContactListNumber()[i], null, sb.toString().substring(0, 159),sentPI, null);
localSmsManager.sendTextMessage(setObj.GetContactListNumber()[i], null, sb.toString().substring(160),sentPI, null);
}
}
catch (SecurityException localSecurityException)
{
Log.e("Error", "Security Exception, SMS permission denied");
return;
}
}
}
else
{
Toast.makeText(hcontext,"please select a number",Toast.LENGTH_SHORT).show();
}
}
}
任意のヘルプははるかに高く評価されるだろう、事前にありがとうございます!
[この非同期タスク](https://github.com/selcukcihan/tahlil/blob/master/app/src/main/java/com/selcukcihan/android/tahlil/HttpPerformingTask.java)を見てください。 http要求を実行します。あなたは基本的に 'AsyncTask'を拡張したクラスを作成し、' onPreExecute'では 'onPostExecute'ハンドラで' dismiss'するべき進捗ダイアログを作成します。 'AsyncTask'は' doInBackground'メソッドの中で 'SmsHandler'を呼び出します。 –