2013-10-24 38 views
1

我試圖根據用戶的活動接收位置,即如果用戶仍然位置更新較少,並且他正在步行或駕駛更新更快。我已經寫了一個服務來接收位置更新,並在接收主要活動廣播的意圖的onCreate()方法中放置了一個廣播接收器。這些廣播意圖帶有一串字符串,告訴我的廣播接收機用戶的「活動」。但是這個接收器從來沒有收到意圖,所以我無法設置locationRequest時間,基於此我將通過適當的locationRequest到服務。使用Google Play服務的活動識別和位置更新

任何機構可以告訴和幫助爲什麼在onCreate服務的廣播接收器可能沒有被調用。謝謝。

public class MyActivityRecognition extends Activity implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener { 

    private ActivityRecognitionClient arclient; 
    private PendingIntent pIntent; 
    private BroadcastReceiver receiver; 
    private TextView tvActivity; 

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

     int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (resp == ConnectionResult.SUCCESS) { 
      arclient = new ActivityRecognitionClient(this, this, this); 
      arclient.connect(); 

     } else { 
      Toast.makeText(this, "Please install Google Play Service.", 
        Toast.LENGTH_SHORT).show(); 
     } 

     receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String u = intent.getStringExtra("Activity"); 
       String v = "Activity :" + intent.getStringExtra("Activity") 
         + " " + "Confidence : " 
         + intent.getExtras().getInt("Confidence") + "\n"; 
       tvActivity.setText(v); 


       Intent activityIntent = new Intent(); 
       intent.setAction("com.example.useractivity"); 
       intent.putExtra("ACTIVITY", u); 
       sendBroadcast(activityIntent); 
      } 
     }; 

服務類如下:

public class LocationService extends Service implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private static final int MILLISECONDS_PER_SECOND = 1000; 

    private static final int FASTEST_INTERVAL_IN_SECONDS = 5; 

    private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND 
      * FASTEST_INTERVAL_IN_SECONDS; 


    LocationRequest mLocationRequest; 
    LocationClient mLocationClient; 
    boolean mUpdatesRequested; 
    String mActivity="Still"; 
    private BroadcastReceiver myReceiver; 

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

     myReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 

       String action = intent.getAction(); 
       if(action.equals("com.example.useractivity")){ 
        mActivity = intent.getExtras().getString("ACTIVITY"); 

       } 

      } 
     }; 

     IntentFilter filter = new IntentFilter(); 
     filter.addAction("com.example.useractivity"); 
     registerReceiver(myReceiver, filter); 

     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

     if (mActivity.equalsIgnoreCase("Still") 
       || mActivity.equalsIgnoreCase("Tilting") 
       || mActivity.equalsIgnoreCase("Unknown")) { 
      mLocationRequest.setInterval(30*1000); 

     } else if (mActivity.equalsIgnoreCase("On Foot") 
       || mActivity.equalsIgnoreCase("On Bicycle")) { 
      mLocationRequest.setInterval(20*1000); 

     } else if (mActivity.equalsIgnoreCase("In Vehicle")) { 
      mLocationRequest.setInterval(10*1000); 

     } 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 

     mLocationClient = new LocationClient(getApplicationContext(), this, this); 
     mUpdatesRequested = true; 

     mLocationClient.connect(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     String latitude = Double.toString(location.getLatitude()); 
     String longitude = Double.toString(location.getLongitude()); 

     String msg = "Updated Location: " + latitude + "," + longitude; 
     Toast.makeText(getApplicationContext(), msg, 
       Toast.LENGTH_SHORT).show(); 
     System.out.println(Double.toString(location.getLatitude()) + "," 
         + Double.toString(location.getLongitude())); 

     SaveData sd = new SaveData(getApplicationContext()); 
     sd.save(mActivity, latitude, longitude); 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 
     if (mUpdatesRequested) { 

      mLocationClient.requestLocationUpdates(mLocationRequest, this); 

     } 
    } 
+0

該代碼總是會調用'onLocationChanged'每當UserState running.You需要設置布爾值false,禁止調用'mLocationClient.connect();'直到用戶狀態改變 – androidXP

回答

2

我知道我很晚這個問題,但我想我反正回答。

在MyActivityRecognition中,您似乎正在使用傳遞給onReceive的'intent'變量,我認爲您希望設置action和extraIntent。見下文。

Intent activityIntent = new Intent(); 
intent.setAction("com.example.useractivity"); 
intent.putExtra("ACTIVITY", u); 
sendBroadcast(activityIntent); 

也許你的意思是這樣的:

Intent activityIntent = new Intent(); 
activityIntent.setAction("com.example.useractivity"); 
activityIntent.putExtra("ACTIVITY", u); 
sendBroadcast(activityIntent); 

由於不使用activityIntent您的服務將不會收到額外的和行動,因此你想要它做的事不做。

我希望這可以幫助,謝謝:)(這是我第一次嘗試回答,請註明它正確的,如果它是。)

相關問題