Android 区分两个单独SMS的交付报告';s
在服务中,当广播接收器监听发送报告并在环路中发送短信时,如何区分发送的每条短信的发送报告? 这类似于: 除了我认为他在一个活动中使用它,而我在一个getIntent()无效的服务中使用它 编辑2:发布我的代码Android 区分两个单独SMS的交付报告';s,android,sms,report,Android,Sms,Report,在服务中,当广播接收器监听发送报告并在环路中发送短信时,如何区分发送的每条短信的发送报告? 这类似于: 除了我认为他在一个活动中使用它,而我在一个getIntent()无效的服务中使用它 编辑2:发布我的代码 public class CheckServer extends Service { public String snumber[] = new String[10]; public JSONArray array; public int onStartCommand(I
public class CheckServer extends Service
{
public String snumber[] = new String[10];
public JSONArray array;
public int onStartCommand(Intent intent,int flags, int startid)
{
// Do useful things.
ServiceAction SA = new ServiceAction();
SA.execute();
try
{
SA.get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
new startSending().execute();
scheduleNextUpdate();
return START_STICKY;
}
public class startSending extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... params)
{
String no,message;
try
{
for (int i = 0; i < array.length(); i++)
{
JSONObject row;
row = array.getJSONObject(i);
snumber[i] = row.getString("sno");
no = row.getString("no");
message = row.getString("message");
sendSMS(no,message,snumber[i]);
}
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
}
private void scheduleNextUpdate()
{
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// The update frequency should often be user configurable. This is not.
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 1 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}
public class ServiceAction extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... arg0)
{
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try
{
request.setURI(new URI("http://www.somesite.com/sms/getsms"));
response = client.execute(request);
String result = convertStreamToString(response.getEntity().getContent());
array = new JSONArray(result);
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
}
public static String convertStreamToString(InputStream inputStream) throws IOException
{
if (inputStream != null)
{
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try
{
Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
int n;
while ((n = reader.read(buffer)) != -1)
{
writer.write(buffer, 0, n);
}
}
finally
{
inputStream.close();
}
return writer.toString();
}
else
{
return "";
}
}
public void sendSMS(String number,String message,String serialnum)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS 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;
}
unregisterReceiver(this);
}
}, new IntentFilter(SENT));
String DELIVERED = "SMS_DELIVERED";
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, Integer.parseInt(serialnum), delivered, 0);
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_SHORT).show();
updateSMSStatus USS = new updateSMSStatus();
USS.execute(intent.getStringExtra("Msgnum"));
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();
break;
}
unregisterReceiver(this);
}
},
new IntentFilter(DELIVERED));
ContentValues values = new ContentValues();
values.put("address", number);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
SmsManager smsMngr = SmsManager.getDefault();
smsMngr.sendTextMessage(number, null, message, sentPI, deliveredPI);
}
public class updateSMSStatus extends AsyncTask<String,Void,Void>
{
@Override
protected Void doInBackground(String... params) {
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try
{
Log.i("SMS APP", "MyClass.getView() — Serial Number = " + params[0]);
request.setURI(new URI("http://www.somesite.com/sms/updatesmsstatus?uname=someone&sno="+params[0]));
response = client.execute(request);
String result = convertStreamToString(response.getEntity().getContent());
Log.i("SMS APP","Update SMS Status is :"+result);
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
公共类CheckServer扩展服务
{
公共字符串snumber[]=新字符串[10];
公共JSONArray数组;
公共int onStartCommand(Intent Intent、int标志、int startid)
{
//做有用的事情。
ServiceAction SA=新的ServiceAction();
SA.execute();
尝试
{
SA.get();
}
捕捉(中断异常e)
{
e、 printStackTrace();
}
捕获(执行例外)
{
e、 printStackTrace();
}
新建startSending().execute();
scheduleNextUpdate();
返回开始时间;
}
公共类startSending扩展异步任务
{
@凌驾
受保护的Void doInBackground(Void…参数)
{
字符串否,消息;
尝试
{
对于(int i=0;idelivered.putExtra("MsgNum", serialnum);
USS.execute(intent.getStringExtra("Msgnum"));
public static final String EXTRA_MSGNUM = "MsgNum";
delivered.putExtra(EXTRA_MSGNUM, serialnum);
USS.execute(intent.getStringExtra(EXTRA_MSGNUM));
String DELIVERED = "SMS_DELIVERED";
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
Integer.parseInt(serialnum), delivered, 0);
String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(DELIVERED);
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
Integer.parseInt(serialnum), delivered,
PendingIntent.FLAG_ONE_SHOT);
public class MessageStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is called when the status of your SMS changes (delivery or send status)
// .. put your code here ..
}
}
<receiver android:name=".MessageStatusReceiver" />
String DELIVERED = "SMS_DELIVERED" + serialnum; // Unique ACTION every time
Intent delivered = new Intent(context, MessageStatusReceiver.class);
delivered.setAction(DELIVERED ); // Set action to ensure unique PendingIntent
delivered.putExtra("MsgNum", serialnum);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this,
Integer.parseInt(serialnum), delivered,
PendingIntent.FLAG_ONE_SHOT);