gpt4 book ai didi

java - 如何停止并重新启动 Android 定位服务

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:00 25 4
gpt4 key购买 nike

我在 MainActivity 中实现了以下服务,并且我想通过 Buttonclick 启动和重新启动 Service。但是当我单击“停止”时,服务仍然发送位置更新!当我按下停止按钮时,如何立即停止服务?我的错在哪里?

服务等级:

public class LocationNotifyService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

final static String MY_ACTION = "MY_ACTION";

LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
public static Location mCurrentLocation;
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;

private static final int TWO_MINUTES = 1000 * 60 * 2;


public static final String MOVEMENT_UPDATE = "com.client.gaitlink.AccelerationService.action.MOVEMENT_UPDATE";
public static final String ACCELERATION_X = "com.client.gaitlink.AccelerationService.ACCELERATION_X";
public static final String ACCELERATION_Y = "com.client.gaitlink.AccelerationService.ACCELERATION_Y";
public static final String ACCELERATION_Z = "com.client.gaitlink.AccelerationService.ACCELERATION_Z";
public static final String ACCELERATION_PACE = "com.client.gaitlink.AccelerationService.ACCELERATION_PACE";
public static final String ACCELERATION_TIME = "com.client.gaitlink.AccelerationService.ACCELERATION_TIME";



@Override
public void onCreate()
{

Log.d("create", "create");

//show error dialog if GoolglePlayServices not available
if (isGooglePlayServicesAvailable()) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(5); /* min dist for location change, here it is 10 meter */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

mGoogleApiClient.connect();
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

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

//Check Google play is available or not
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
return ConnectionResult.SUCCESS == status;
}


@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}

@Override
public void onConnectionSuspended(int i) {

}

protected void startLocationUpdates()
{
Log.d("start","start");

try {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} catch (IllegalStateException e) {}
}

@Override
public void onLocationChanged(Location location)
{



Log.d("changed", "changed");

Intent intent = new Intent(MOVEMENT_UPDATE);
intent.setAction(MY_ACTION);
intent.putExtra(ACCELERATION_X, location.getLongitude());
intent.putExtra(ACCELERATION_Y, location.getLatitude());
intent.putExtra(ACCELERATION_Z, location.getAltitude());
intent.putExtra(ACCELERATION_PACE, location.getSpeed());
intent.putExtra(ACCELERATION_TIME, location.getTime());

sendBroadcast(intent);

Log.d("send","send");
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

public boolean stopService(Intent name)
{
// TODO Auto-generated method stub
mGoogleApiClient.disconnect();

return super.stopService(name);

}

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


}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

Button start;
Button stop;
TextView xyz;

MyReceiver myReceiver;

double accelerationX;
double accelerationY;
double accelerationZ;
float pace;
List<Double> pace_list = new ArrayList<Double>();


Location oldLoc = new Location("locationOld");
Location newLoc = new Location("locationNew");

double distance = 0;
long time = 0;

String temp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);

xyz = (TextView) findViewById(R.id.xyz);

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Log.d("click", "click");

myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(LocationNotifyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);


Intent intent = new Intent(MainActivity.this,
LocationNotifyService.class);
startService(intent);

//startService(new Intent(MainActivity.this, LocationNotifyService.class));
}
});

stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,
LocationNotifyService.class);
stopService(intent);
Log.d("stop", "stop");
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();


if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

private class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

Log.d("revcive", "recive");


accelerationX = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_X, 0);
accelerationY = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_Y, 0);
accelerationZ = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_Z, 0);
pace = intent.getFloatExtra(LocationNotifyService.ACCELERATION_PACE, 0);
pace = (float) (pace * 3.6);
time = intent.getLongExtra(LocationNotifyService.ACCELERATION_TIME, 0);

pace_list.add((double) pace);


SimpleDateFormat s = new SimpleDateFormat("yyyyMMddhhmmss");
String format = s.format(new Date());


temp += format + " | " + accelerationX + " | " + accelerationY + " | " + accelerationZ + " | " + Collections.max(pace_list) + "\n";

xyz.setText(temp);


}
}

}

Manifest.xml

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<action android:name="com.client.gaitlink.CommunicationService.action.ACTIVITY_STATUS_UPDATE" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".LocationNotifyService"
android:exported="false"/>


</application>

最佳答案

我猜你忘了在 stopService() 中添加以下行

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

将其添加到 stopService 中 -

**// Missing @Override**
@Override
public boolean stopService(Intent name) {

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
return super.stopService(name);

}

修改MainActivity.java如下 -

public class MainActivity extends AppCompatActivity {

Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {

intent = new Intent(MainActivity.this,
LocationNotifyService.class);

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

...
startService(intent);

}
});


stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

stopService(intent);
}
});

} // end of class MainActivity

您需要注释以下来自 stop.setOnClickListenerstart.setOnClickListener 的代码-

Intent intent = new Intent(MainActivity.this, LocationNotifyService.class);

* 您必须使用与启动服务相同的 Intent 来停止服务。

关于java - 如何停止并重新启动 Android 定位服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com