【问题标题】:how to get latitude and longitude value in BroadcastReceiver service?如何在 BroadcastReceiver 服务中获取经纬度值?
【发布时间】:2019-06-07 21:47:35
【问题描述】:

所以我制作了一个应用程序,如果按下手机的电源按钮 5 次,该应用程序就会运行,该应用程序将在后台执行类似于打开 url 的操作。现在的问题是我想在那个时候提取纬度和经度值。 代码是

LockService 类

public class LockService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        final BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
        return super.onStartCommand(intent, flags, startId);
    }
    public class LocalBinder extends Binder {
        LockService getService() {
            return LockService.this;
        }
    }
}

屏幕接收器

public class ScreenReceiver extends BroadcastReceiver {

    LocationManager locationManager;
    static int countPoweroff =0;

    public static boolean wasScreenOn = true;
    int lol;
    @Override
    public void onReceive(final Context context, final Intent intent) {

        Log.e("LOB","onReceive");
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            // do whatever you need to do here

            wasScreenOn = false;
            countPoweroff++;
            Log.e("LOB",""+countPoweroff);
            Log.e("LOB","wasScreenOn"+wasScreenOn);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // and do whatever you need to do here
            if (countPoweroff == 3){
                Log.e("LOB","userpresent");
                Log.e("LOB","wasScreenOn"+wasScreenOn);
                String url = "http://www.stackoverflow.com";
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setData(Uri.parse(url));
                context.startActivity(i);

                Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
                } else {
                    //deprecated in API 26
                    vibrator.vibrate(500);
                }
                countPoweroff =0;

            }
            wasScreenOn = true;

        }else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){




        }
    }

在主要活动中运行服务

startService(new Intent(getApplicationContext(), LockService.class));

我在主要活动中使用了基本的 Locationmanager 代码,该代码仅在活动打开时才有效。

【问题讨论】:

    标签: java android geolocation gps broadcastreceiver


    【解决方案1】:

    您放入活动中的任何代码当然只会在活动运行时执行。如果您的位置管理器处于活动状态且活动未运行,则您无法获取位置信息。当司机关闭引擎并去午休时,您的公共汽车将不会移动到任何地方。在活动中做任何与 Ux 无关的事情都是糟糕的设计。我不知道您的实现细节,因此很难说更多。您应该在服务中实现位置管理器并使用 Callable 或 CompletableFuture 将结果发送到活动。

    【讨论】:

    • 谷歌应用商店里有很多应用程序都有这样的功能,比如你按下电源按钮 5 次或者你摇动你的手机,应用程序会在不打开位置坐标的情况下向紧急号码发送文本应用程序,我的意思是应用程序在后台运行并执行这些任务,我也想做同样的事情。
    • 那么在这种情况下,即使用户没有打开应用程序,他们也会在始终运行的后台服务中获取位置。另一点是,请记住主线程的服务运行,因此,您必须显式创建一个工作线程来进行位置收集,因为它可能会在较差的 GPS 或网络定位场景中锁定主线程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多