【问题标题】:How can I stop and restart Android Location Service如何停止和重新启动 Android 定位服务
【发布时间】:2016-02-04 13:30:31
【问题描述】:

我在我的 MainActivity 中实现了以下服务,我想通过 Buttonclick 启动和重新启动 Service。但是当我点击停止时,Service 仍然发送Location updates!当我按下停止按钮时,如何立即停止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>

【问题讨论】:

  • 服务是否延迟停止?还是服务根本没有停止?
  • 使用处理程序而不是使用服务。
  • @Saritha G 为什么是处理程序?我也想在后台恢复更新!
  • @AlbAtNf 我不知道!我认为这是一个延迟,但我该如何解决这个延迟?
  • 数据会在你启动locationListener时更新。甚至您可以通过删除处理程序的回调来停止 locationListener。

标签: java android service location broadcastreceiver


【解决方案1】:

我猜你忘了在 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);

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

【讨论】:

  • 我添加了这行代码,但按下停止按钮后,我仍然收到更新!我的错是什么?
  • 我已经添加了您建议的 cahnges,但没有任何区别!该服务仍在发送更新!
  • @madrid11:你错过了第 mth 的 stopService() 覆盖。检查我的编辑:)
猜你喜欢
  • 2011-03-07
  • 2019-04-25
  • 2013-01-29
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多