2016-02-04 131 views
1

我在我的MainActivity中實施了以下服務,並且我想通過Buttonclick啓動並重新啓動Service。但是當我點擊停止Service仍然發送Location updates!當我按下停止按鈕時,如何立即停止Service?我的故障是什麼?如何停止並重新啓動Android定位服務

服務類:

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> 
+0

服務是否延遲停止?或者服務沒有停止? – AlbAtNf

+0

使用處理程序而不是使用服務。 –

+0

@Saritha G爲什麼處理?我想要在後臺恢復更新! – madrid11

回答

0

我想你忘記了以下行裏面添加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); 

*您必須使用啓動服務來停止服務的相同意圖IVCE。

+0

我添加了這行代碼,但按下停止按鈕後,我仍然收到更新!我的錯是什麼? – madrid11

+0

@ madrid11:檢查編輯看看它是否工作。 – kevz

+0

我已經添加了你的建議cahnges,但它沒有區別!該服務仍在發送更新! – madrid11

相關問題