我的Android应用程序存在问题。我需要一个应用程序,每10分钟将后台GPS位置发送到服务器(这里我有WCF服务工作)。应用程序即使关闭也需要发送数据。所以我创建了一个服务(用于测试目的),它在服务器上发送给我时间。但当我插入我的代码以获取我的服务中的GPS位置时,一切都失败了(意外停止)。
我已经知道你需要放一些接收器或其他东西,但到目前为止还没有。如果有人能帮助我,我会很高兴。
<?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>
答案 0 :(得分:34)
我已经在我的应用程序中完成了这件事,就像你问的一样。
在我的服务中我添加了这个:
@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);
}
}
注意:看到我使用 MyApplication.getAppContext(); ,因为我正在使用自己的Application类来添加上下文。 然后我有一个BroadcastReceiver:
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
}
}
Application类:
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;
}
}
同样在你的清单中你需要添加两个:
<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>
检测服务是否正在运行的方法我在第一个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);
}
显然,如果服务正在运行(onStart)或不运行(onDestroy),则需要控制SharedPreferences(设置)以将locationService变量设置为 true / false 。