在预定位置附近从Android发送短信(短信)...
实施例。我进入大学或从大学进入我的Android设备检查我的当前位置,如果它与预定位置匹配,那么我的设备发送自动短信到其他没有。
任何好友都有与此相关的想法或代码..
谢谢..答案 0 :(得分:3)
我从SMS util中输入了整个代码。你应该看看 sendSms 函数。 util允许您监视传入的短信并跟踪您发送的短信(如果您想这样做)。
下一部分是处理位置更新。如何做到这一点的最佳方式取决于很多事情。您可以通过LocatinProviders(GPS,无线或被动)获取位置或从TelephonyManager读取单元信息。下面你有一些关于它们的细节:
LocationProvider:
细胞。
哪个选项更适合您?
package android.commons;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public final class SmsModem extends BroadcastReceiver {
private static final String SMS_DELIVER_REPORT_ACTION = "android.commons.SMS_DELIVER_REPORT";
private static final String SMS_DELIVER_REPORT_TOKEN_EXTRA = "token";
private static final String TAG = SmsModem.class.getSimpleName();
private final Context context;
private final SmsManager smsManager;
private final SmsModemListener listener;
private final Map<String, Integer> pendingSMS = new HashMap<String, Integer>();
public interface SmsModemListener {
public void onSMSSent(String token);
public void onSMSSendError(String token, String errorDetails);
public void onNewSMS(String address, String message);
}
public SmsModem(Context c, SmsModemListener l) {
context = c;
listener = l;
smsManager = SmsManager.getDefault();
final IntentFilter filter = new IntentFilter();
filter.addAction(SMS_DELIVER_REPORT_ACTION);
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
context.registerReceiver(this, filter);
}
public void sendSms(String address, String message, String token) {
if ( message != null && address != null && token != null) {
final ArrayList<String> parts = smsManager.divideMessage(message);
final Intent intent = new Intent(SMS_DELIVER_REPORT_ACTION);
intent.putExtra(SMS_DELIVER_REPORT_TOKEN_EXTRA, token);
final PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final ArrayList<PendingIntent> intents = new ArrayList<PendingIntent>();
for ( int i = 0 ; i < parts.size() ; i++ ) {
intents.add(sentIntent);
}
pendingSMS.put(token, parts.size());
smsManager.sendMultipartTextMessage(address, null, parts, intents, null);
}
}
public void clear() {
context.unregisterReceiver(this);
}
@Override
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();
if ( action.equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
final String address = messages[i].getDisplayOriginatingAddress();
final String message = messages[i].getDisplayMessageBody();
listener.onNewSMS(address, message);
}
}
} else if ( action.equalsIgnoreCase(SMS_DELIVER_REPORT_ACTION)) {
final int resultCode = getResultCode();
final String token = intent.getStringExtra(SMS_DELIVER_REPORT_TOKEN_EXTRA);
Log.d(TAG, "Deliver report, result code '" + resultCode + "', token '" + token + "'");
if ( resultCode == Activity.RESULT_OK ) {
if ( pendingSMS.containsKey(token) ) {
pendingSMS.put(token, pendingSMS.get(token).intValue() - 1);
if ( pendingSMS.get(token).intValue() == 0 ) {
pendingSMS.remove(token);
listener.onSMSSent(token);
}
}
} else {
if ( pendingSMS.containsKey(token) ) {
pendingSMS.remove(token);
listener.onSMSSendError(token, extractError(resultCode, intent));
}
}
}
}
private String extractError(int resultCode, Intent i) {
switch ( resultCode ) {
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
if ( i.hasExtra("errorCode") ) {
return i.getStringExtra("errorCode");
} else {
return "Unknown error. No 'errorCode' field.";
}
case SmsManager.RESULT_ERROR_NO_SERVICE:
return "No service";
case SmsManager.RESULT_ERROR_RADIO_OFF:
return "Radio off";
case SmsManager.RESULT_ERROR_NULL_PDU:
return "PDU null";
default:
return "really unknown error";
}
}
}