后台服务需要在服务器发送GPS位置后台、位置、服务器、GPS

2023-09-12 22:43:56 作者:为伊憔悴

我有问题,我的Andr​​oid应用程序。我需要将在后台GPS定位每隔10分钟发送到服务器(在这里我有一个工作的WCF服务)的应用程序。应用程序需要发送,即使关闭数据。所以,我创建了一个服务(用于测试目的),其给我的时间在服务器上。但是,当我插入我的code获取GPS位置,在我服务的一切失败(意外停止)。

我有芦苇,你需要把一些接收器或东西,但没有那么远的作品。因此,我将非常高兴。如果有人能帮助我。

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.biromatik.GPS
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =4/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:标签=@字符串/ APP_NAME
            机器人:名称=ETMGPSServiceActivity。>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
    <应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:名称=。MyApplication的>
    <服务
        机器人:启用=真
        机器人:名称=MonitorService。>
    < /服务>
    <接收器
        机器人:启用=真
        机器人:名称=LocationReceiver。>
        <意向滤光器>
            <作用机器人:名称=com.biromatik.intent.action.LOCATION/>
        &所述; /意图滤光器>
    < /接收器>
< /用途>


     <使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
    <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.SEND_SMS>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>

<使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>




< /舱单>
 

解决方案

我在我的应用程序做同样的事情,你问这样做。

在我的服务我加入这样的:

  @覆盖
公共无效ONSTART(意向意图,诠释startId){

    super.onStart(意向,startId);
    addLocationListener();
}

私人无效addLocationListener()
{
    triggerService =新主题(新的Runnable(){
        公共无效的run(){
            尝试{
                活套prepare(); //初始化当前线程作为一个活套。
                LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

                标准C =新标准();
                c.setAccuracy(Criteria.ACCURACY_COARSE);

                最终字符串提供程序= lm.getBestProvider(C,真正的);

                MyLocationListener myLocationListener =新MyLocationListener();
                lm.requestLocationUpdates(供应商,60万,0,myLocationListener);
                Log.d(LOC_SERVICE,服务运行!);
                Looper.loop();
            }赶上(例外前){
                ex.printStackTrace();
            }
        }
    },LocationThread);
    triggerService.start();
}

公共静态无效updateLocation(位置定位)
{
    上下文appCtx = MyApplication.getAppContext();

    双纬度,经度;

    纬度= location.getLatitude();
    经度= location.getLongitude();

    意图filterRes =新意图();
    filterRes.setAction(xxx.yyy.intent.action.LOCATION);
    filterRes.putExtra(纬度,纬度);
    filterRes.putExtra(经度,经度);
    appCtx.sendBroadcast(filterRes);
}


类MyLocationListener实现LocationListener的
{

    @覆盖
    公共无效onLocationChanged(位置定位)
    {
        updateLocation(位置);
    }
}
 
openGPS.cn 高精度IP定位原理,定位误差说明

注:请参阅我使用 MyApplication.getAppContext(); 那是因为我用我自己的应用程序类添加上下文的 然后,我有这样一个BroadcastReceiver:

 公共类LocationReceiver扩展的BroadcastReceiver {

    双纬度,经度;

    @覆盖
    公共无效的onReceive(最终上下文的背景下,最终的意图calledIntent)
    {
        Log.d(LOC_RECEIVER,位置好评!);

        纬度= calledIntent.getDoubleExtra(纬度,-1);
        经度= calledIntent.getDoubleExtra(经度,-1);

        updateRemote(纬度,经度);

    }

    私人无效updateRemote(最终双纬度,最终双经度)
    {
        //这里你可以把你的AsyncTask更新位置您的服务器
    }
}
 

应用程序类:

 公共类MyApplication的扩展应用{


    私有静态上下文的背景下;

    / *(非Javadoc中)
     * @see android.app.Application#的onCreate()
     * /
    @覆盖
    公共无效的onCreate(){
        MyApplication.context = getApplicationContext();
    }

    公共静态上下文getAppContext(){
        返回MyApplication.context;
    }
}
 

另外,在你的清单,你需要添加两个:

 <应用
    机器人:图标=@可绘制/图标
    机器人:标签=@字符串/ APP_NAME
    机器人:名称=。MyApplication的>

    //你的活动在这里...

    <服务
        机器人:启用=真
        机器人:名称=LocationService。>
    < /服务>

    <接收器
        机器人:启用=真
        机器人:名称=LocationReceiver。>
        <意向滤光器>
            <作用机器人:名称=xxx.yyy.intent.action.LOCATION/>
        &所述; /意图滤光器>
    < /接收器>
< /用途>
 

方法来检测,如果该服务正在运行我这样做是我的第一个活动:

  / **
 *检查位置服务。
 * /
私人无效checkLocationService()
{
    Log.d(CHECK_SERVICE,服务运行:+(settings.getBoolean(locationService,FALSE)是:NO));

    如果(settings.getBoolean(locationService,FALSE))
        返回;

    意图mServiceIntent =新的意图(这一点,LocationService.class);
    startService(mServiceIntent);
}
 

显然,你需要控制的共享preferences(设置)设置locationService变量的 true,如果该服务正在运行(ONSTART)或没有/假的(的onDestroy)。

I have problem with my Android application. I need an application that will send in background GPS location every 10 minutes to the server (here i have WCF service that work). Application need to send data even if it closed. So i created a service (for test purposes) that send me time on server. But when i insert my code for getting GPS location in my service everything fail (stopped unexpectedly).

I have reed that you need to put some receiver or something but nothing so far works. SO i will be very pleased i someone can help me.

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.biromatik.GPS"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ETMGPSServiceActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".MyApplication" >
    <service
        android:enabled="true"
        android:name=".MonitorService" >
    </service>
    <receiver
        android:enabled="true"
        android:name=".LocationReceiver" >
        <intent-filter >
            <action android:name="com.biromatik.intent.action.LOCATION" />
        </intent-filter>
    </receiver>
</application>


     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />




</manifest>

解决方案

I've done this in my app to do the same thing you asked.

In my service I added this:

@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);
    addLocationListener();
}

private void addLocationListener()
{
    triggerService = new Thread(new Runnable(){
        public void run(){
            try{
                Looper.prepare();//Initialise the current thread as a looper.
                lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

                Criteria c = new Criteria();
                c.setAccuracy(Criteria.ACCURACY_COARSE);

                final String PROVIDER = lm.getBestProvider(c, true);

                MyLocationListener myLocationListener = new MyLocationListener();
                lm.requestLocationUpdates(PROVIDER, 600000, 0, myLocationListener);
                Log.d("LOC_SERVICE", "Service RUNNING!");
                Looper.loop();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }, "LocationThread");
    triggerService.start();
}

public static void updateLocation(Location location)
{
    Context appCtx = MyApplication.getAppContext();

    double latitude, longitude;

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    Intent filterRes = new Intent();
    filterRes.setAction("xxx.yyy.intent.action.LOCATION");
    filterRes.putExtra("latitude", latitude);
    filterRes.putExtra("longitude", longitude);
    appCtx.sendBroadcast(filterRes);
}


class MyLocationListener implements LocationListener
{

    @Override
    public void onLocationChanged(Location location)
    {
        updateLocation(location);
    }
}

NOTE: See that I use MyApplication.getAppContext(); that's because I'm using my own Application class to add the context. And then I have a BroadcastReceiver with this:

public class LocationReceiver extends BroadcastReceiver {

    double latitude, longitude;

    @Override
    public void onReceive(final Context context, final Intent calledIntent)
    {
        Log.d("LOC_RECEIVER", "Location RECEIVED!");

        latitude = calledIntent.getDoubleExtra("latitude", -1);
        longitude = calledIntent.getDoubleExtra("longitude", -1);

        updateRemote(latitude, longitude);

    }

    private void updateRemote(final double latitude, final double longitude )
    {
        //HERE YOU CAN PUT YOUR ASYNCTASK TO UPDATE THE LOCATION ON YOUR SERVER
    }
}

the Application class:

public class MyApplication extends Application {


    private static Context context;

    /* (non-Javadoc)
     * @see android.app.Application#onCreate()
     */
    @Override
    public void onCreate() {            
        MyApplication.context=getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

Also in your manifest you need to add both:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name=".MyApplication" >

    //YOUR ACTIVITIES HERE...

    <service
        android:enabled="true"
        android:name=".LocationService" >
    </service>

    <receiver
        android:enabled="true"
        android:name=".LocationReceiver" >
        <intent-filter >
            <action android:name="xxx.yyy.intent.action.LOCATION" />
        </intent-filter>
    </receiver>
</application>

Method to detect if the service is running I do this on my first Activity:

/**
 * Check location service.
 */
private void checkLocationService()
{
    Log.d("CHECK_SERVICE", "Service running: " + (settings.getBoolean("locationService", false)?"YES":"NO"));

    if(settings.getBoolean("locationService", false))
        return;

    Intent mServiceIntent = new Intent(this, LocationService.class);
    startService(mServiceIntent);
}

Obviously you need to control the SharedPreferences (settings) to set the locationService variable to true/false if the service is running (onStart) or not (onDestroy).