【问题标题】:A service which will run in the background all time?一项将始终在后台运行的服务?
【发布时间】:2012-04-06 07:58:56
【问题描述】:

考虑到 2 个人拥有该应用。当其中一个用户想知道另一个用户的位置时,他会发送一条短信(内容可以是任何内容或特定内容)。现在当另一个用户收到短信时(如果可能,他不应该知道他收到了它)。然后他的位置被发送回请求它的用户。 希望解释清楚。

public void onReceive(Context context, Intent intent) 
{   
//this stops notifications to others
this.abortBroadcast();

//---get the SMS message passed in---
Bundle bundle = intent.getExtras();   
SmsMessage[] msgs = null;
String str = "";            
if (bundle != null)
{
    //---retrieve the SMS message received---
    Object[] pdus = (Object[]) bundle.get("pdus");
    msgs = new SmsMessage[pdus.length];            
    for (int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
        str += "SMS from " + msgs[i].getOriginatingAddress();
        from = msgs[i].getOriginatingAddress();
        str += " :";
        str += msgs[i].getMessageBody().toString();
        msg = msgs[i].getMessageBody().toString();
        str += "\n"; 
    }
    if(checksomething){
        //make your actions
        //and no alert notification and sms not in inbox
    }
    else{
        //continue the normal process of sms and will get alert and reaches inbox
        this.clearAbortBroadcast();
    }
  }

我得到了这个代码...我应该对此进行哪些更改...?

【问题讨论】:

  • :那么通过扩展Service Class来创建Service有什么问题呢?
  • 我想创建一个始终运行的服务。并始终检查用户位置。你能帮我写代码吗?

标签: android service sms location


【解决方案1】:

您需要的不是服务,而是位于您想要其位置的用户一侧的广播接收器。

public class SmsReceiver extends BroadcastReceiver {


    private static final String ACTION_SMS_RECEIVED = 
        "android.provider.Telephony.SMS_RECEIVED";


    @Override
    public void onReceive(Context context, Intent intent) {


            if(intent.getAction().equals(ACTION_SMS_RECEIVED)){
                //your code to get location here.
            }

    }
}

【讨论】:

  • 所以我只需要添加这个...?
  • 只要手机收到短信,广播接收器就会运行。您将不得不编写代码来读取位置并在 if 检查中将其发送回。此外,您需要在清单文件中定义 SmsReceiver 类。
  • 但这会帮助我自动发回带有位置的短信吗?
  • 这只是概述了如何拦截传入的短信。要获取地理位置,请查看此问题 - stackoverflow.com/questions/1513485/… 并发送短信请参阅此问题 stackoverflow.com/questions/8578689/…
  • 还有一件事……我需要在我的应用程序中创建发送短信的东西吗……?或普通的(意思是在电话里)。我想我必须这样做,然后通常它会继续发送位置......我是否必须制作一些特定的端口来发送接收短信。
【解决方案2】:

通过扩展Service.like 来创建普通服务

    public class LocalService extends Service {
        @Override
        public void onCreate() {
  // SEND USER LOCATION
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i("LocalService", "Received start id " + startId + ": " + intent);
            return START_STICKY;
        }
        @Override
        public void onDestroy() {
        }
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
       }

开始您的服务:

  public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
           // CHECK SMS IF IT IS FOR SEND LOCTION OR ANY OTHER SMS
            if (action.equals(ACTION_SMS_RECEIVED)) {
               //START SERVICE HERE
            }

        }
    };

并为ACTION_BOOT_COMPLETED ,ACTION_SCREEN_ON,ACTION_SCREEN_OFF注册BroadcastReceiver来处理服务的启动和停止

也许有帮助:-)

【讨论】:

  • 谢谢。然后得到消息后获取位置呢..?
  • 您想在特定时间间隔后在消息中发送用户位置对吗?
  • 不是特定的时间间隔,但是当我向用户发送消息时,作为回报,我应该得到位置。
  • 那你为什么要一直在后台运行服务呢?然后使用IntentService 或在收到短信时启动它或收集用户位置然后发送它?
  • 或者如果你想在设备上存储用户位置,意味着每 2 分钟后,当收到短信时你发送它,然后使用 ACTION_BOOT_COMPLETED ,ACTION_SCREEN_ON,ACTION_SCREEN_OFF 启动服务或注册一个 ACTION_SMS_RECEIVED 用于短信
猜你喜欢
  • 2011-02-03
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多