Java 安卓:位置;活动和服务
我正在开发一个应用程序,它应该获取当前位置,在设备位于某个半径后,应该向应用程序显示一些消息。 我有几个问题。 我正在尝试使用获取位置Java 安卓:位置;活动和服务,java,android,geolocation,Java,Android,Geolocation,我正在开发一个应用程序,它应该获取当前位置,在设备位于某个半径后,应该向应用程序显示一些消息。 我有几个问题。 我正在尝试使用获取位置 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 50, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*6
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);
}
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.string.intent\获取的消息\位置\应该在位置大约为LAT LONG后触发
然后我尝试创建BroadcastReceiver类:
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());
}
}
并在MyActivity类中注册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);
...
但当我在一个很长的位置时,什么都不会发生。发生了什么?
也许我应该在AndroidManifest.xml中注册reciver?但具体是怎样的呢?我正在使用此代码获取用户的当前位置
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();
}
}
上面的调用方法在得到位置时删除位置更新,这样可以节省电池。
我希望这对您有所帮助。1.您应该在此处检查答案,以便使用计时器通过GPS打开/关闭位置查找: 它将每X秒启动一个计时器。在计时器处理过程中,将请求位置更新,在检索到位置后,它将停止GPS侦听器 2.您应该在接近警报逻辑结束后发送意图。然后,您可以注册一个侦听该意图的广播接收器,并进行处理。意图机制提供了活动/服务之间通信的松散耦合方式 您可以找到如何将意图广播到广播接收器(应用程序中的一个新组件,它将接收意图,并相应地对其进行处理)的信息(在您的情况下,可以从邻近警报中找到)。它还显示了如何在清单中设置接收器以侦听您的自定义意图 如果我使用requestLocationUpdates,则GPS始终工作。这是对电池的浪费。也许有什么办法,比如,暂停几分钟?我的意思是每5分钟使用一次GPS,例如从一个地点到下一个地点。如何实现这一点 addProximityAlert()每4分钟使用一次GPS。如果您想每x分钟使用一次GPS,请运行一项服务并每x分钟获取一次,然后手动检查该区域是否存在GPS 如果这里有一些课程-一些活动 同学们,那我怎么知道 这件事到底叫它什么?因为 当ProximityAlert被触发时,I 需要调用某些活动的函数 也我怎么能这样做 不完全确定你在问什么 下面是如何使用挂起的意图
PendingIntent pendingIntent;
Intent intent = new Intent();
intent.setClass(mContext, yourActivity.class);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
:
这使启动的活动成为任务的根活动。如果您正在启动的活动的任务已在运行,则新活动将不会启动;取而代之的是,当前任务将简单地显示在屏幕的前面,并显示它最后的状态
PendingIntent pendingIntent;
Intent intent = new Intent();
intent.setClass(mContext, yourActivity.class);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);