- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我是安卓新手。
我想在广播接收器中获取 GPS 位置,但它显示错误。
我的代码是:
public void onReceive(Context context, Intent intent) {
LocationManager locManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
// errors in getSystemService method
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locListener);
Location loc = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d(" **location**", " location" + loc.getLatitude());
}
问题:
最佳答案
是的,两者都是可能的。
您的服务带有计时器,可在一段时间内向位置接收器发送请求:
public class SrvPositioning extends Service {
// An alarm for rising in special times to fire the
// pendingIntentPositioning
private AlarmManager alarmManagerPositioning;
// A PendingIntent for calling a receiver in special times
public PendingIntent pendingIntentPositioning;
@Override
public void onCreate() {
super.onCreate();
alarmManagerPositioning = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(
ReceiverPositioningAlarm.ACTION_REFRESH_SCHEDULE_ALARM);
intentToFire.putExtra(ReceiverPositioningAlarm.COMMAND,
ReceiverPositioningAlarm.SENDER_SRV_POSITIONING);
pendingIntentPositioning = PendingIntent.getBroadcast(this, 0,
intentToFire, 0);
};
@Override
public void onStart(Intent intent, int startId) {
try {
long interval = 60 * 1000;
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timetoRefresh = SystemClock.elapsedRealtime();
alarmManagerPositioning.setInexactRepeating(alarmType,
timetoRefresh, interval, pendingIntentPositioning);
} catch (NumberFormatException e) {
Toast.makeText(this,
"error running service: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this,
"error running service: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
this.alarmManagerPositioning.cancel(pendingIntentPositioning);
ReceiverPositioningAlarm.stopLocationListener();
}
}
你的接收者和一个听众。可以在您的 Activity 中使用监听器来通知您已准备好新位置:
public class ReceiverPositioningAlarm extends BroadcastReceiver {
public static final String COMMAND = "SENDER";
public static final int SENDER_ACT_DOCUMENT = 0;
public static final int SENDER_SRV_POSITIONING = 1;
public static final int MIN_TIME_REQUEST = 5 * 1000;
public static final String ACTION_REFRESH_SCHEDULE_ALARM =
"org.mabna.order.ACTION_REFRESH_SCHEDULE_ALARM";
private static Location currentLocation;
private static Location prevLocation;
private static Context _context;
private String provider = LocationManager.GPS_PROVIDER;
private static Intent _intent;
private static LocationManager locationManager;
private static LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
try {
String strStatus = "";
switch (status) {
case GpsStatus.GPS_EVENT_FIRST_FIX:
strStatus = "GPS_EVENT_FIRST_FIX";
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
strStatus = "GPS_EVENT_SATELLITE_STATUS";
break;
case GpsStatus.GPS_EVENT_STARTED:
strStatus = "GPS_EVENT_STARTED";
break;
case GpsStatus.GPS_EVENT_STOPPED:
strStatus = "GPS_EVENT_STOPPED";
break;
default:
strStatus = String.valueOf(status);
break;
}
Toast.makeText(_context, "Status: " + strStatus,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onLocationChanged(Location location) {
try {
Toast.makeText(_context, "***new location***",
Toast.LENGTH_SHORT).show();
gotLocation(location);
} catch (Exception e) {
}
}
};
// received request from the calling service
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "new request received by receiver",
Toast.LENGTH_SHORT).show();
_context = context;
_intent = intent;
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_REQUEST, 5, locationListener);
Location gotLoc = locationManager
.getLastKnownLocation(provider);
gotLocation(gotLoc);
} else {
Toast t = Toast.makeText(context, "please turn on GPS",
Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
Intent settinsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
settinsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(settinsIntent);
}
}
private static void gotLocation(Location location) {
prevLocation = currentLocation == null ? null : new Location(
currentLocation);
currentLocation = location;
if (isLocationNew()) {
OnNewLocationReceived(location);
Toast.makeText(_context, "new location saved",
Toast.LENGTH_SHORT).show();
stopLocationListener();
}
}
private static boolean isLocationNew() {
if (currentLocation == null) {
return false;
} else if (prevLocation == null) {
return true;
} else if (currentLocation.getTime() == prevLocation.getTime()) {
return false;
} else {
return true;
}
}
public static void stopLocationListener() {
locationManager.removeUpdates(locationListener);
Toast.makeText(_context, "provider stoped", Toast.LENGTH_SHORT)
.show();
}
// listener ----------------------------------------------------
static ArrayList<OnNewLocationListener> arrOnNewLocationListener =
new ArrayList<OnNewLocationListener>();
// Allows the user to set a OnNewLocationListener outside of this class
// and react to the event.
// A sample is provided in ActDocument.java in method: startStopTryGetPoint
public static void setOnNewLocationListener(
OnNewLocationListener listener) {
arrOnNewLocationListener.add(listener);
}
public static void clearOnNewLocationListener(
OnNewLocationListener listener) {
arrOnNewLocationListener.remove(listener);
}
// This function is called after the new point received
private static void OnNewLocationReceived(Location location) {
// Check if the Listener was set, otherwise we'll get an Exception
// when we try to call it
if (arrOnNewLocationListener != null) {
// Only trigger the event, when we have any listener
for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) {
arrOnNewLocationListener.get(i).onNewLocationReceived(
location);
}
}
}
}
监听器接口(interface):
import android.location.Location;
public interface OnNewLocationListener {
public abstract void onNewLocationReceived(Location location);
}
在你的 Activity 中只获得一分:
protected void btnGetPoint_onClick() {
Intent intentToFire = new Intent(
ReceiverPositioningAlarm.ACTION_REFRESH_SCHEDULE_ALARM);
intentToFire.putExtra(ReceiverPositioningAlarm.COMMAND,
ReceiverPositioningAlarm.SENDER_ACT_DOCUMENT);
sendBroadcast(intentToFire);
OnNewLocationListener onNewLocationListener = new OnNewLocationListener() {
@Override
public void onNewLocationReceived(Location location) {
// use your new location here then stop listening
ReceiverPositioningAlarm.clearOnNewLocationListener(this);
}
};
// start listening for new location
ReceiverPositioningAlarm
.setOnNewLocationListener(onNewLocationListener);
}
编辑:
如果您想在 Activity 中启动服务:
this.startService(new Intent(this, SrvPositioning.class));
类似地,您可以在服务中定义一个监听器来接收接收者找到的位置
编辑
在 Manifest 中添加以下行
<service
android:name="org.mabna.order.services.SrvPositioning"
android:enabled="true" />
<receiver android:name="org.mabna.order.receivers.ReceiverPositioningAlarm" >
<!-- this Broadcast Receiver only listens to the following intent -->
<intent-filter>
<action android:name="org.mabna.order.ACTION_REFRESH_SCHEDULE_ALARM" />
</intent-filter>
</receiver>
关于android - 在广播接收器/或服务中获取 GPS 位置以广播接收器数据传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7709030/
媒体存在于外部服务器上,我想在我的 Actor 接收器上播放该媒体( Google's CastReferencePlayer 的修改版本)。接收器与该服务器持续通信(通过长轮询),并在需要播放某个媒
我想将我的 PyQt4 应用程序移植到 PyQt5,但遇到了一个微妙的问题。 有时我会检查自定义 QThread 对象 (worker) 是否仍然连接了一些特定的信号,我在 PyQt4 中已经这样做了
假设我们有 n 台设备,n 为偶数。每个设备都可以作为发射器 (T) 或接收器 (R)。对于每个设备 i,我们都给定了 2 个数字,Ti 和 Ri。 Ti 是设备用作发射器时的成本,Ri 是设备用作接
我想在 android 中创建 airplay,其中我的 android 设备将用作 airplay 服务器(接收器),而 iPhone 设备将用作接收器。我在我的应用程序中使用了 jmdns,它是
简单问题 - 我可以将单个 BroadcastReceiver 注册到多个 Intent 操作吗?这是我正在考虑的: 所以在 myRecei
假设我在 2 个应用程序(应用程序 A 和应用程序 B)的 list 中有以下接收器: 在每个应用程序中,我想创建一个 PendingIntent(如果不存在
我正在尝试向接收方应用程序的关闭事件添加逻辑,但每次发送方断开连接时,调试器都会关闭并且不会执行任何逻辑(例如发送一些 HttpRequest)。我的一段代码: this.context.addEve
我们正在使用flume,我需要将一些日志消息收集到rabbitmq 中。我找到了一个来源 implementation从rabbitmq读取消息,但我找不到可以将消息写入rabbit的接收器。所以我想
我遇到了一个远程异常: “这个远程代理没有 channel 接收器,这意味着服务器没有注册的服务器 channel 正在监听,或者这个应用程序没有合适的客户端 channel 来与服务器通信。” th
我是 WebRTC 的新手,并试图弄清楚如何在浏览器之外创建一个程序,该程序接收 WebRTC 音频流并将其输出到扬声器上。 是否有适用于 Java 或 C# 的 WebRTC 库? 该接收器将在 l
我正在创建一个简单的 Spring Boot 应用程序,我想接收发送到 AMQP(Rabbit)交换(来自另一个应用程序)的消息。 我收到一条错误消息,指出我要接收的队列不存在。当我看 http://
我将 EJB 3.0 与 JBoss AS 7.1.1 Final 一起使用。当我尝试将客户端连接到服务器时出现此错误: Aug 15, 2012 12:05:00 PM org.jboss.ejb.
我正在为 Google Cast SDK 使用 React Native 包装器,但无法从发送方向接收方发送消息。我能够转换媒体或暂停并恢复它。问题仅在于自定义消息。我的自定义消息监听器永远不会在接收
我正在开发自定义 Serilog 接收器,它继承自 PeriodicBatchingSink 并调用我的网络服务将数据写入数据库,使用类似于 Serilog.Sinks.Seq 的模式。使用此代码作为
我想为安卓手机开发一个定制的 FM radio 应用程序,里面有 FM 接收芯片。 通过研究,我发现 FM 接收器通常由 BroadComm 开发。 主要的安卓手机制造商——三星、HTC、索尼爱立信是
我的 android list 中有一个 Intent 接收器,但我想让用户有机会选择他/她是否希望应用程序在特定状态下自动启动。到目前为止,我一直在使用带有广播接收器的服务,但我真的很想删除这个服务
我正在做一个如果我们摇动手机就锁屏的应用程序,我已经写了屏幕关闭的代码,但现在的问题是我需要一个广播接收器来检查屏幕是关闭还是打开,我怎么能做吗? 最佳答案 如果您需要在特定时刻检查屏幕是否关闭或打开
我让 MySQL 在一页上生成具有相同操作和提交按钮的表单。表格的数量各不相同。它们在提交时都调用同一个 PHP 文件。另外,我有一个 PHP 文件,它在提交时收集数据。请参见下面的示例。 问题是当提
谁能给我一个例子,说明如何在 Activity 类中正确取消注册 LocalBroadcastManager 接收器? Android 开发人员培训建议这样做: @Override publ
有问题的代码: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
我是一名优秀的程序员,十分优秀!