【问题标题】:Implement ResultCallback to MainActivity from GeofenceTransitionsIntentService从 GeofenceTransitionsIntentService 实现 ResultCallback 到 MainActivity
【发布时间】:2017-03-30 04:55:35
【问题描述】:

我是 android 开发新手,正在尝试学习地理围栏。我相信我的意图服务可以正常工作,我可以注册我的地理围栏并在理论上接收到服务的转换;但是,我有兴趣从意图服务中获取数据并将其发送回MainActivity.class,以便可以利用它来执行一些任务。我看到很多创建通知的示例,但我不想要通知,而是简单地将转换类型和触发地理围栏传递回 MainActivity 类。

我的 GeofenceTransitionsIntentService.class 在下面,我假设我需要实现某种方式将结果发送回主类,并且我假设主类将需要某种侦听器来接收这些结果,因为他们发布.

public class GeofenceTransitionsIntentService extends IntentService implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
    private GoogleApiClient myGoogleApiClient;

public GeofenceTransitionsIntentService(){
    super(GeofenceTransitionsIntentService.class.getSimpleName());
}

@Override
public void onCreate(){
    super.onCreate();
    myGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geoFenceEvent = GeofencingEvent.fromIntent(intent);
    if (geoFenceEvent.hasError()){
        int errorcode = geoFenceEvent.getErrorCode();
        Log.e("GeofencingApp", "ERROR: " + errorcode);
    } else {
        int transitionType = geoFenceEvent.getGeofenceTransition();

        if (Geofence.GEOFENCE_TRANSITION_ENTER == transitionType){
            myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
            String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();




        } else if (Geofence.GEOFENCE_TRANSITION_EXIT == transitionType) {
            myGoogleApiClient.blockingConnect(100, TimeUnit.MILLISECONDS);
            String triggeredGeofenceID = geoFenceEvent.getTriggeringGeofences().get(0).getRequestId();
            Toast.makeText(getApplicationContext(), "EXIT: " + triggeredGeofenceID, Toast.LENGTH_LONG).show();
        }
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为您可以将 Intent 与 putExtra 一起使用

    startService(new Intent(this,MainActivity.class).putExtra("Key","Data));
    

    或回调模式查看此答案

    What is callback in Android?

    【讨论】:

      【解决方案2】:

      我想出了如何简单成功地完成任务。

      GeofenceTransitionService 的代码很好,MainActivity 的代码也很好。

      在MainActivity中添加一个变量到类中,如下:

      private static MainActivity ins;
      

      然后在 MainActivity 类中创建两个函数:

      public static MainActivity getInstance(){
          return ins;
      }
      
      public void updateMethod(){
          MainActivity.this.runOnUiThread(new Runnable() {
              public void run(){
                  //Do what you want to do on the MainActivity here
              }
          });
      }
      

      它的作用是为 GeofenceTransitionService 提供一个公共方法来调用并将 MainActivity 返回给它,然后在 MainActivity 上执行您想要的任何方法。如果您没有适当的 runOnUiThread,您将在运行时收到一个错误,指出您不允许与来自不同线程的视图进行交互。

      虽然这里没有显示,但您可以轻松地从 GeofenceTransitionsIntentService 添加信息并通过将变量放在 updateMethod 中传递给 MainActivity... 例如:

      public void updateMethod(final Boolean didEnter){
          // insert the above code here to run on the UI thread
      }
      

      此外,您还可以轻松创建多个方法来执行各种任务,这些任务都可以从 Intent 服务访问。

      intent服务端,代码也很简单:

      MainActivity.getInstance().updateMethod([pass stuff here]);
      

      我希望这有助于人们制作一些更强大的地理围栏应用程序。我注意到几乎所有示例都专注于发送通知,还有很多事情可以做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多