合并操作以打开和关闭活动

时间:2012-11-08 17:50:54

标签: android

我有一个正在运行的教程应用程序(很棒!)但是我想为它添加一些额外的技巧。有效地,应用程序将广播接收智能手机的当前位置,有人可能不希望一直发生这种位置。我的示例代码显示了3个硬编码方案:

  • 第一块代码已被注释掉,但显示了用于广播用户位置的真实代码。
  • 第二个块是活动的,目前正在播放“我不是在告诉你”的消息。
  • 第三个是一些示例代码,它已被注释掉代码,让人们认为我是Google总部。

所以(在下面显示的代码之外)我添加了On和Off按钮以启用第一个和第二个按钮之间的切换,这些触发onClickOn和onClickOff动作。我想知道如何在这组代码中以编程方式包含一些onClickOn / onClickOff操作。让我前进的一些指导方针非常有帮助。

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {
        if (loc != null) {

            /* 1st Chunk - Commented out to deactivate broadcasting current location.
            *
            //---send a SMS containing the current location---
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(senderTel, null,
                    senderTel+" is currently located at:\n\n" +
                    "http://maps.google.com/maps?t=m&f=q&q=loc:"
                    + loc.getLatitude() + ","
                    + loc.getLongitude() + "&z=18", null, null);
            */ End 1st chunk

            // 2nd chunk - The active chunk of code currently being broadcast.
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(senderTel, null,
                    senderTel+" has suspended broadcasting his whereabouts for the time bieng but ", null, null);
            // End 2nd chunk

             /* 3rd chunk - The original sample message focused the map on 
              * Google HQ Tulsa and is inactive.

            // Used for capturing sample app screen shots---                
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(senderTel, null,
                    "http://maps.google.com/maps?z=18&t=m&f=q&q=loc:37.422006,-122.084095", null, null);
             * End 3rd chunk
             */

            //---stop listening for location changes---
            lm.removeUpdates(locationListener);                
        }
    }

0 个答案:

没有答案