2016-10-17 7 views
1

私はこのクラスのGPS_Serviceを以下に用意しています。それは完璧に働いています。 MainActivityからGPS_Service.gpsupdatedelay変数の値を変更して、GPS_ServiceのrequestLocationUpdates経由でGPSが呼び出される頻度を調整できます。 MainActivityから、GPS_Service.gpsupdatedelay = 10000で値を変更します。私は、変数値の変更を行うためにGPS_Serviceのいくつかの側面を呼び出す必要があると思いますか?私はGPS_Service.onCreate()を呼び出す方法を見ることができないし、私はそれを行うべきだとも思わない?Android Java - change requestLocationUpdates refresh in class

質問:gpsupdatedelayの値を調整した後、requestLocationUpdatesのパラメータで変数の値を変更して実際にGPS周波数を調整するにはどうすればよいですか?

ありがとうございます。

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.widget.Toast; 

public class GPS_Service extends Service { 
    private LocationListener listener; 
    private LocationManager locationManager; 
    public static Integer gpsupdatedelay; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    @Override 
    public void onCreate() { 
     Log.e("GPS","onCreate"); 
     listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       Intent i = new Intent("location_update"); 
       //i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude()); 

       i.putExtra("coordinate1",location.getLatitude()); 
       i.putExtra("coordinate2",location.getLongitude()); 
       Log.e("GPS","sendBroadcast"); 
       sendBroadcast(i); 
      } 
      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 
      } 
      @Override 
      public void onProviderEnabled(String s) { 
      } 
      @Override 
      public void onProviderDisabled(String s) { 
       Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(i); 
      } 
     }; 
     locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     //noinspection MissingPermission 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener); 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(locationManager != null){ 
      //noinspection MissingPermission 
      locationManager.removeUpdates(listener); 
     } 
    } 
} 

答えて

1

実際には、gpsupdatedelay変数を静的変数として定義しないでください。それを静的として定義し、アクティビティから直接アクセスするのではなく、更新する必要があるときに新しい値をIntent Extraとして送信します。

public class GPS_Service extends Service { 
    private LocationListener listener; 
    private LocationManager locationManager; 
    public Integer gpsupdatedelay = 10000; //NOT static! 

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

    //Added: 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     int newGpsUpdateDelay = intent.getIntExtra("new_gps_update_delay", -99); 
     if (newGpsUpdateDelay != -99 && locationManager != null) { 
      gpsupdatedelay = newGpsUpdateDelay; 
      //noinspection MissingPermission 
      locationManager.removeUpdates(listener); 
      //noinspection MissingPermission 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener); 
     } 
     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     Log.e("GPS","onCreate"); 
     listener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       Intent i = new Intent("location_update"); 
       //i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude()); 

       i.putExtra("coordinate1",location.getLatitude()); 
       i.putExtra("coordinate2",location.getLongitude()); 
       Log.e("GPS","sendBroadcast"); 
       sendBroadcast(i); 
      } 
      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 
      } 
      @Override 
      public void onProviderEnabled(String s) { 
      } 
      @Override 
      public void onProviderDisabled(String s) { 
       Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(i); 
      } 
     }; 
     locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     //noinspection MissingPermission 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,gpsupdatedelay,0,listener); 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(locationManager != null){ 
      //noinspection MissingPermission 
      locationManager.removeUpdates(listener); 
     } 
    } 
} 

ために:

最初のステップは、新しい間隔値を更新するonStartCommand()メソッドのオーバーライドを定義古い位置のリスナーの登録を解除し、新たな間隔で位置更新のために再登録しますただ意図を作成、活動から値を更新するために、new_gps_update_delay余分に新しい値を入れて、サービスに更新情報を送信するためにstartService()を呼び出す:

int newDelay = 678; 
    Intent i = new Intent(this, GPS_Service.class); 
    i.putExtra("new_gps_update_delay", newDelay); 
    startService(i); 

これをstartService()メソッドは、サービスがすでに実行中であっても呼び出すことができます。サービスが既に実行されている場合は、基本的にはonStartCommand()メソッドオーバーライドを実行します。迅速な対応のための

If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

+0

ありがとう:the documentation for startService()から

。それは有り難いです。上のコードで変数を更新する簡単な質問。現在位置情報サービスが開始されています。したがって、startService(i)を使用すると、ロケーションサービスが再度開始されて複製されるか、またはstartService(i)によって既存の/実行中のロケーションサービスが停止/再開されるか、startServuce(i)ロケーションサービスの実行には何の影響もありませんか?あなたはおそらく、私はAndroid/Javaには新しいことを伝えることができます。 –

+0

もう一度お世話になります - 推奨された変更でクラスを更新し、MainActivityからのクラスの呼び出しを調整した後、私はこのエラーを受け取りました: "E/AndroidRuntime:FATAL EXCEPTION:main プロセス:com.example.ftonyan.gps4、PID:15469 java.lang.RuntimeException:サービスを作成できませんcom.example.ftonyan.gps6.GPS_Service:java.lang.NullPointerException:nullオブジェクト参照で仮想メソッド 'int java.lang.Integer.intValue()'を呼び出そうとしています android.app.ActivityThread.handleCreateService(ActivityThread.java:3158) " –

+0

私は助けてくれて、私が間違って何をしたのか疑問に思っています。 –