如何在靠近预定义位置时从 Android 发送短信 (SMS)

发布于 2024-11-09 08:22:04 字数 155 浏览 0 评论 0原文

当靠近预定义位置时从 Android 发送短信 (SMS)...

例如。当我进入大学或离开大学时,我的 Android 设备会检查我当前的位置是否与预定义位置匹配,然后我的设备会自动向其他号码发送短信。

任何朋友有与此相关的想法或代码..

谢谢..

Send Text Messages (SMS) From Android When Near A Predefined Location...

Ex. i enter in the college or out from the college that time my Android device check my current position if it match with predefine position then my device send automatic sms to other no.

Any buddy have idea or code related to this ..

thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-11-16 08:22:04

我把完整的代码从我的短信实用程序。您应该看看 sendSms 函数。该实用程序允许您监视传入的短信并跟踪您发送的短信(如果您想这样做)。

下一部分是处理位置更新。最好的方法取决于很多因素。您可以通过 LocatinProviders(GPS、无线或被动)获取位置或从 TelephonyManager 读取手机信息。下面是有关它们的一些详细信息:

  1. LocationProvider:

    • 返回地理纬度/经度数据
    • 如果用户禁用“使用 GPS 卫星”和“用户无线网络”,您将无法读取数据
    • 如果您在建筑物内(那里没有 GPS 信号),您将不愿意获取数据。
    • 您必须等待很长时间才能找到位置。
    • 非常好的准确性。
    • 会大量消耗电池电量。
    • 被动”提供程序可能是一个不错的选择你。
  2. 细胞。

    • 返回设备的邻区信息。
    • 如果您的设备未连接到 gsm/cdma 网络(无 SIM 卡),则无法获取位置。
    • 准确性不太好,但适合您的目的就可以了。
    • 不会太耗电。

哪种选择更适合您?

   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";
                    }
            }
    }

I put whole code from my SMS util. You should have a look at sendSms function. The util allows you to watch for incoming sms and track sms sent by you ( If you want to do that ).

The next part is to handle location updates. The best way how to do it depends on many things. You can obtain location via LocatinProviders ( GPS, wireless or passive ) or read cell info from TelephonyManager. Below you have some details about them:

  1. LocationProvider:

    • returns geo lat/lon data
    • you can not read data if user disabled "Use GPS satellites" and "User wireless networks"
    • you will rather not get data if you are in a building ( no GPS signal there ).
    • you have to wait very long for the location.
    • very good accuracy.
    • can drain battery a lot.
    • "pasive" provider can be a good choice for you.
  2. Cells.

    • returns the neighboring cell information of the device.
    • location is not available if your device is not connected to gsm/cdma network ( no sim card ).
    • not good accuracy but rather for you purpose will be ok.
    • doesn't kill battery so much.

Which option is better for you ?

   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";
                    }
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文