2011-07-06 59 views
0

我正在研究一些應該獲取當前位置的應用程序,並且在設備位於某個半徑之後應該顯示應用程序和一些消息。 我有幾個問題。 我試圖獲取位置使用Android:位置;活動和服務

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 50, locationListener); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*5, 50, locationListener); 
addProximityAlert(LAT,LONG); 

private void addProximityAlert(double latitude, double longitude) 
     { 
      Intent intent = new Intent(this.getApplicationContext(), SOME_CLASS_HERE); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //Does this needed even if apllication/activity is alive? 
      PendingIntent proximityIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); //and does this needed again? 
      locationManager.addProximityAlert(latitude, longitude, Radius, 0, proximityIntent); 
     } 
  1. 如果我使用requestLocationUpdates然後GPS總是工作。這是浪費電池。例如,也許有一些方法可以暫停幾分鐘?我的意思是每5分鐘使用一次GPS,例如從一個位置到下一個位置。如何實現這一點?

  2. 如果SOME_CLASS_HERE - 某些活動類,那麼我怎麼知道這個事件是否確切地調用了它?因爲當ProximityAlert被觸發時,我也需要調用一些活動的功能。我怎麼能這樣做?


UPD:現在我只用活動(我還沒有在服務全部) 我想未來:

Intent intent = new Intent(getString(R.string.intent_message_location_fetched)); 
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 
locationManager.addProximityAlert(LAT, LONG, RADIUS, 0, proximityIntent); 

據我瞭解的意圖與行動R.在位置將在LAT LONG周圍後,應該觸發string.intent_message_location_fetched。

然後我試圖創建BroadcastReciver類:在MyActivity類

public class MyBroadcastReciver extends BroadcastReceiver { 
    MyActivity mainActivity; 
    public MyBroadcastReciver(MyActivity activity) 
    { 
     mainActivity = activity; 
    } 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     mainActivity.ShowToast("Recived : " + intent.getAction()); 
    } 
} 

和註冊reciver:

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     myReciver = new MyBroadcastReciver(this); 
     IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(getString(R.string.intent_message_location_fetched)); 
     registerReceiver(myReciver, intentFilter); 
... 

但是,當我在LAT LONG位置是沒有任何反應。怎麼了? 也許我應該在AndroidManifest.xml註冊reciver?但究竟如何?

回答

1

1.You應該在這裏檢查答案使用計時器通過GPS來開啓/關閉位置查找:android locationManager requestLocationUpdates

它將啓動一個定時器每隔X秒。在計時器處理過程中,會請求位置更新,並且在檢索到位置後,它將停止GPS偵聽器。

2。在接近警報邏輯結束後,您應該發送一個意圖。然後,您可以註冊偵聽該意圖的廣播接收器,並進行處理。意圖機制提供了活動/服務之間鬆散耦合的溝通方式。

你可以找到an example關於廣播接收者(你的應用程序中的一個新組件,它將接收到這個意圖並相應地處理它)的廣播方式。它還顯示瞭如何在清單中設置接收器來偵聽您的自定義意圖。

0

我使用此代碼獲取用戶的當前位置。

public class HomeActivity extends Activity implements LocationListener{ 
public static Context mContext; 
private double latitude, longitude; 
public LocationManager mLocManager; 
// *******This is the new Code start on 11/4/2011 at 3 o'clock 


/** 
* This is the Home Button if user Login then it is move to TennisAppActivity otherwise move to Login 
* 
*/ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    mContext=this; 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.homelayout); 


    mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
      this); 
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 
      0, this); 
    locationUpdate(); 
    ((Button) this.findViewById(R.id.ButtonHome)) 
      .setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 

         startActivity(new Intent(HomeActivity.this, 
           DefaultDisplay.class)); 

       } 
      }); 

    ((Button) this.findViewById(R.id.ButtonProfile)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        if (GUIStatics.boolLoginStatus) { 
         startActivity(new Intent(HomeActivity.this, 
           MyProfile.class)); 
        } else { 
         Intent intent=new Intent(HomeActivity.this, 
           Login.class); 
         intent.putExtra("moveTo","MyProfile"); 
         startActivity(intent); 
        } 
       } 
      }); 

    ((Button) this.findViewById(R.id.ButtonNotifications)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        if (GUIStatics.boolLoginStatus) { 
         startActivity(new Intent(HomeActivity.this, 
           ShowAllNotificationActiviry.class)); 
        } else { 
         Intent intent=new Intent(HomeActivity.this, 
           Login.class); 
         intent.putExtra("moveTo","ShowAllNotificationActiviry"); 
         startActivity(intent); 
        } 
       } 
      }); 

    ((Button) this.findViewById(R.id.ButtonFavorites)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        if (GUIStatics.boolLoginStatus) { 
         startActivity(new Intent(HomeActivity.this, 
           FavoritesActivity.class)); 
        } else { 
         Intent intent=new Intent(HomeActivity.this, 
           Login.class); 
         intent.putExtra("moveTo","FavoritesActivity"); 
         startActivity(intent); 
        } 
       } 
      }); 

      ((Button) this.findViewById(R.id.ButtonMore)) 
      .setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
         startActivity(new Intent(HomeActivity.this, 
           MoreListActivity.class)); 
       } 
      }); 

} 

public void locationUpdate() 
{ 
    CellLocation.requestLocationUpdate(); 
} 


public void getAddress(double lat, double lng) { 
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
     Address obj = addresses.get(0); 
     String add = obj.getAddressLine(0); 
     GUIStatics.currentAddress = obj.getSubAdminArea() + "," 
       + obj.getAdminArea(); 
     GUIStatics.latitude = obj.getLatitude(); 
     GUIStatics.longitude = obj.getLongitude(); 
     GUIStatics.currentCity= obj.getSubAdminArea(); 
     GUIStatics.currentState= obj.getAdminArea(); 
     add = add + "\n" + obj.getCountryName(); 
     add = add + "\n" + obj.getCountryCode(); 
     add = add + "\n" + obj.getAdminArea(); 
     add = add + "\n" + obj.getPostalCode(); 
     add = add + "\n" + obj.getSubAdminArea(); 
     add = add + "\n" + obj.getLocality(); 
     add = add + "\n" + obj.getSubThoroughfare(); 

     Log.v("IGA", "Address" + add); 
     // Toast.makeText(this, "Address=>" + add, 
     // Toast.LENGTH_SHORT).show(); 

     // TennisAppActivity.showDialog(add); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
} 



public void onLocationChanged(Location location) { 
    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 
    GUIStatics.latitude=location.getLatitude(); 
    GUIStatics.longitude= location.getLongitude(); 
    Log.v("Test", "IGA" + "Lat" + latitude + " Lng" + longitude); 
    //mLocManager.r 

    getAddress(latitude, longitude); 
    if(location!=null) 
    { 

    mLocManager.removeUpdates(this); 
    } 
    // Toast.makeText(this, "Lat" + latitude + " Lng" + longitude, 
    // Toast.LENGTH_SHORT).show(); 
} 


public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 
    Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show(); 
    Intent intent = new Intent(
      android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
    startActivity(intent); 
} 


public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 


public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    if(arg1 == 
      LocationProvider.TEMPORARILY_UNAVAILABLE) { 
            Toast.makeText(HomeActivity.this, 
      "LocationProvider.TEMPORARILY_UNAVAILABLE", 
      Toast.LENGTH_SHORT).show(); 
         } 
         else if(arg1== LocationProvider.OUT_OF_SERVICE) { 
            Toast.makeText(HomeActivity.this, 
      "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
         } 

} 

}

在這段代碼中你會發現

mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, 本);

在該方法的第一個參數是提供,II-timeforupdate位置,III minDistance纔會對更新請求

mLocManager.removeUpdates(this); 

位置的上述呼叫方法刪除更新時,它得到這樣你就節省電池。 我希望這對你有幫助。

1

如果我使用requestLocationUpdates,那麼GPS總是工作。這是浪費電池。例如,也許有一些方法可以暫停幾分鐘?我的意思是每5分鐘使用一次GPS,例如從一個位置到下一個位置。如何實現這一點?

addProximityAlert()每4分鐘使用一次GPS。如果您想每x分鐘使用一次GPS,請運行服務並每隔x分鐘獲取single shot fixes,並手動檢查該區域是否存在。

如果SOME_CLASS_HERE - 一些活動 類,那麼我怎麼能知道是否 正是這一事件叫的嗎?因爲 當ProximityAlert被觸發時,I 也需要調用某個活動的功能 。我怎麼能這樣做?

不能完全確定你在這裏問什麼

下面是如何使用掛起的意圖

PendingIntent pendingIntent; 
    Intent intent = new Intent(); 
    intent.setClass(mContext, yourActivity.class); 
    pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); 

FLAG_ACTIVITY_NEW_TASK: 這使得發起活動任務的根系活力。如果某個任務已經在爲您正在啓動的活動運行,則則不會啓動新活動;取而代之的是,當前的任務將被簡單地帶到屏幕的最前面的狀態。