2017-03-26 15 views
0

我想通过使用服务获取GPS位置在后台运行。当我点击“开始”按钮时,每隔X个时间间隔GPS位置就会发送到屏幕,并在单击“停止”按钮时停止。这是我第一次运行这个项目,但是一旦我关闭了应用程序并再次打开它,它就不再工作了。我已经清理了该项目并再次运行,没有显示任何内容。我错过了什么?谷歌作为服务的GPS作为一次只有

这是活动代码

import android.Manifest; 
import android.app.Dialog; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.ActivityInfo; 
import android.content.pm.PackageManager; 
import android.location.Location; 

import android.location.LocationManager; 
import android.os.Build; 
import android.support.annotation.NonNull; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.GoogleApiAvailability; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.location.FusedLocationProviderApi; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.ConnectionResult; 

import java.text.DateFormat; 
import java.util.Date; 

public class testLocationActivity extends AppCompatActivity 
{ 

    protected LocationHelper locationHelper; 

    protected TextView textView; 
    protected Button btn_start; 
    protected Button btn_stop; 
    protected BroadcastReceiver broadcastReceiver; 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     if(broadcastReceiver == null) 
     { 
      broadcastReceiver = new BroadcastReceiver() { 
       @Override 
       public void onReceive(Context context, Intent intent) { 

        textView.append("\n" + intent.getExtras().get("coordinates")); 

       } 
      }; 
     } 
     registerReceiver(broadcastReceiver, new IntentFilter("location_update")); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     if(broadcastReceiver != null) 
     { 
      unregisterReceiver(broadcastReceiver); 
     } 
    } 

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

     setSupportActionBar(toolbar); 
     getSupportActionBar().setTitle("Test Location"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

      this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     textView = (TextView) findViewById(R.id.textView); 
     btn_start = (Button) findViewById(R.id.button); 
     btn_stop = (Button) findViewById(R.id.button2); 


     if(!runtime_permissions()) 
     { 
      enable_buttons(); 
     } 

    } 

    private void enable_buttons() 
    { 
     btn_start.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), LocationHelper.class); 
       startService(intent); 
      } 
     }); 

     btn_stop.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), LocationHelper.class); 
       stopService(intent); 
      } 
     }); 
    } 

    private boolean runtime_permissions() 
    { 
     if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 100); 
      return true; 
     } 

     return true; 


    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
    { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if(requestCode == 100) 
     { 
      if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) 
      { 
       enable_buttons(); 
      } 
      else 
      { 
       runtime_permissions(); 
      } 
     } 
    } 
} 

这是一个扩展服务

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
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.location.LocationListener; 

import com.firebase.client.Firebase; 
import com.google.firebase.auth.FirebaseAuth; 

public class LocationHelper extends Service 
{ 

    private LocationListener listener; 
    private LocationManager locationManager; 

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

    @Override 
    public void onCreate() 
    { 
     listener = new LocationListener() { 

      @Override 
      public void onLocationChanged(Location location) 
      { 
       Intent intent = new Intent("location_update"); 

       intent.putExtra("coordinates", location.getLongitude()+ " " + location.getLatitude()); 
       sendBroadcast(intent); 

      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras)  { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) 
      { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK); 

       startActivity(intent); 
      } 
     }; 

     locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0, listener); 

      //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3000,0, listener); 
    } 

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

     if(locationManager != null) 
     { 
      locationManager.removeUpdates(listener); 
     } 
    } 
} 

的LocationHelper.java类任何帮助或指针赞赏!

回答

0

对于那些有兴趣的人,我找出了错误。我必须在runtime_permissions函数中返回true。当我改变第二次返回返回false时,一切正常!

相关问题