我试着写一个Android应用程序,第二个短信到达我的手机,它上传它或发送到我的服务器在互联网上,我写了获取新的短信号码和身体部分,但现在我不知道应该它将senderNum和Message写入文件并上传或将其直接发送到我的互联网服务器,任何想法?请告诉我samle代码。感谢..
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
String sindrome = senderNum + message;
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
sendSMS("15555215554", sindrome);
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
private byte[] getBytes() {
// TODO Auto-generated method stub
return null;
}
private OutputStream openFileOutput(String string, int modePrivate) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:1)
这是一个想法,你可以使用MySQL数据库和PHP文件。
PHP文件将关心在DB中保存短信,它可能类似于
<?php
// You should consider the idea to put everything in POST request and add "key" values to avoid someone to
// send to your server 1000 sms from the web with just a page reload
$from = $_GET['from'];
$body = $_GET['body'];
if (!is_numeric($from))
{
// Invalid input
// Do nothing
exit();
}
// connect to the MYSQL server
$mysqli = new MySQLi(...);
/*
* DB:
* PHONE NUMBER | BODY
* X | X
*
* You can anyway add more info like sending time etc.
*/
// run the query insert
if ($stmt = $mysqli->prepare("INSERT INTO sms (number, body) VALUES (?, ?)"))
{
$stmt->bind_param("is", $from, $body);
$stmt->execute();
$stmt->close();
}
// now you have in your database the sms
// close mysqli connection
$mysqli->close();
?>
然后,您可以使用AsyncTask或Handler从Android应用程序运行请求。
好吧,也许我错过了关闭一些资源......但一切都应该没问题。
// As number i think SenderNumber is the number of who send the SMS
// message is here the message is saved
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
// OK
// Here you have senderNum and message
HttpClient httpClient = new DefaultHttpClient();
// Since it's a GET reuqest you can pass params directly
// but as i said, a POST could be more better
HttpGet httpGet = new HttpGet("YOUR_URL_HERE?number=" + number + "&body=" + message);
String responseBody;
HttpResponse response;
try
{
// We run the request
response = httpClient.execute(httpGet);
// now i need to know if everything is ok
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
// OK!
if (statusCode == 200)
{
responseBody = EntityUtils.toString(response.getEntity());
// Now in responseBody you have the response of the request
// If you don't need it, remove it
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
它应该可以帮助您了解如何执行代码。