0

ジオフェンスを完全に新しくしました。ジオフェンスの設定に関する次のチュートリアルに続きました:http://io2015codelabs.appspot.com/codelabs/geofences#4ジオフェンスに入ったときに簡単なメッセージをトーストする方法は?

コードが実行されましたが、ジオフェンスに入ると何も起こりません...ジオフェンスに入るか、またはジオフェンスを離れるときに、どのようにトーストしますか?私のコードは、ガイドのものとまったく同じです。だから私はあなたたちがいくつかの例で私を助けてくれることを願っています。 I

ここ

は、地理フェンスクラスです:

public class Constants 
{ 
    public static final long GEOFENCE_EXPIRATION_IN_MILLISECONDS = 12 * 60 * 60 * 1000; 
    public static final float GEOFENCE_RADIUS_IN_METERS = 20; 

    public static final HashMap<String, LatLng> LANDMARKS = new HashMap<String, LatLng>(); 
    static { 
     // San Francisco International Airport. 
     LANDMARKS.put("Westwood", new LatLng(34.067313, -118.461164)); //34.065767, -118.459658 LANDMARKS.put("Westwood", new LatLng(34.065767, -118.459658)); 

     // Googleplex. 
     LANDMARKS.put("Japantown", new LatLng(37.785281,-122.4296384)); 

     // Test 
     LANDMARKS.put("SFO", new LatLng(37.621313,-122.378955)); 
    } 
} 
+0

は、次のステップに従ってください、と 'どこかonHandleIntent'で乾杯を示しました。 – stkent

+0

@stkentだから、onHandleIntentに単純なToastステートメントを書くだけですか? –

+0

ガイドがやったようにonHandleIntentを実装したのですが、何も起こっていません...? –

答えて

1

は、ガイドのコードは示しています。ここ

public class GeoFence extends AppCompatActivity implements 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     ResultCallback<Status> 
{ 
    protected ArrayList<Geofence> mGeofenceList; 
    protected GoogleApiClient mGoogleApiClient; 
    private Button mAddGeofencesButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_geo_fence); 
     mAddGeofencesButton = (Button) findViewById(R.id.add_geofences_button); 
     // Empty list for storing geofences. 
     mGeofenceList = new ArrayList<Geofence>(); 

     // Get the geofences used. Geofence data is hard coded in this sample. 
     populateGeofenceList(); 

     // Kick off the request to build GoogleApiClient. 
     buildGoogleApiClient(); 

    } 

    public void populateGeofenceList() { 
     for (Map.Entry<String, LatLng> entry : Constants.LANDMARKS.entrySet()) { 
      mGeofenceList.add(new Geofence.Builder() 
        .setRequestId(entry.getKey()) 
        .setCircularRegion(
          entry.getValue().latitude, 
          entry.getValue().longitude, 
          Constants.GEOFENCE_RADIUS_IN_METERS 
        ) 
        .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS) 
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | 
          Geofence.GEOFENCE_TRANSITION_EXIT) 
        .build()); 
     } 
    } 
    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 
    public void addGeofencesButtonHandler(View view) { 
     if (!mGoogleApiClient.isConnected()) { 
      Toast.makeText(this, "Google API Client not connected!", Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     try { 
      LocationServices.GeofencingApi.addGeofences(
        mGoogleApiClient, 
        getGeofencingRequest(), 
        getGeofencePendingIntent() 
      ).setResultCallback(this); // Result processed in onResult(). 
     } catch (SecurityException securityException) { 
      // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
     } 
    } 
    private GeofencingRequest getGeofencingRequest() { 
     GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
     builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
     builder.addGeofences(mGeofenceList); 
     return builder.build(); 
    } 
    private PendingIntent getGeofencePendingIntent() { 
     Intent intent = new Intent(this, GTIS.class); 
     // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling addgeoFences() 
     return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 
    @Override 
    public void onConnected(Bundle connectionHint) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // Do something with result.getErrorCode()); 
    } 
    @Override 
    public void onConnectionSuspended(int cause) { 
     mGoogleApiClient.connect(); 
    } 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (!mGoogleApiClient.isConnecting() || !mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient.isConnecting() || mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    public void onResult(Status status) 
    { 
     if (status.isSuccess()) { 
      Toast.makeText(
        this, 
        "Geofences Added", 
        Toast.LENGTH_SHORT 
      ).show(); 
     } else { 
      Toast.makeText(this,"No geo fence :(",Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

は私の定数クラスがされGeofenceTransitionsIntentServiceクラスここ

public class GTIS extends IntentService 
{ 
    protected static final String TAG = "GeofenceTransitionsIS"; 

    public GTIS() { 
     super(TAG); // use TAG to name the IntentService worker thread 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     GeofencingEvent event = GeofencingEvent.fromIntent(intent); 
     if (event.hasError()) { 
      Log.e(TAG, "GeofencingEvent Error: " + event.getErrorCode()); 
      String description = getGeofenceTransitionDetails(event); 
      sendNotification(description); 
      return; 
     } 
    } 
    private static String getGeofenceTransitionDetails(GeofencingEvent event) { 
     String transitionString = 
       GeofenceStatusCodes.getStatusCodeString(event.getGeofenceTransition()); 
     List triggeringIDs = new ArrayList(); 
     for (Geofence geofence : event.getTriggeringGeofences()) { 
      triggeringIDs.add(geofence.getRequestId()); 
     } 
     return String.format("%s: %s", transitionString, TextUtils.join(", ", triggeringIDs)); 
    } 

    private void sendNotification(String notificationDetails) { 
     // Create an explicit content Intent that starts MainActivity. 
     Intent notificationIntent = new Intent(getApplicationContext(), Geofence.class); 

     // Get a PendingIntent containing the entire back stack. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(GeoFence.class).addNextIntent(notificationIntent); 
     PendingIntent notificationPendingIntent = 
       stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Get a notification builder that's compatible with platform versions >= 4 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

     // Define the notification settings. 
     builder.setColor(Color.RED) 
       .setContentTitle(notificationDetails) 
       .setContentText("Click notification to return to App") 
       .setContentIntent(notificationPendingIntent) 
       .setAutoCancel(true); 

     // Fire and notify the built Notification. 
     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, builder.build()); 
    } 

} 

ですNotificationは、Toastではありません。あなたは何のエラーはあなたがする必要がどのような

が起こらないので、あなたの場合にはトリガされませんif(event.hasError())文の中sendNotification()を呼び出しているので、それはあなたのために働いていないことである:

再び
@Override 
protected void onHandleIntent(Intent intent) { 
    GeofencingEvent event = GeofencingEvent.fromIntent(intent); 
    if (event.hasError()) { 
     Log.e(TAG, "GeofencingEvent Error: " + event.getErrorCode()); 
    } 
    String description = getGeofenceTransitionDetails(event); 
    sendNotification(description); 
} 

、このコードが表示されるでしょう

  1. アプリもちょうど示すジオフェンス遷移とトーストの時点でアクティブではないかもしれない○:これらの理由で実際の生活のアプリでToastより理にかなってNotificationあなたにとっては奇妙に見えることはありません。
  2. 通知は、あなたがより多くの情報(タイトル、テキスト)を表示し、多分あなたのアプリに戻って送ることができます
  3. それは、ユーザを読み取り、あなたはまだしたい場合は、その利便性

でそれを却下する機会を与えてくれます

  • とにかくToastを表示するために、あなたはそれをあなたがActivityでそれを行うのと同じ方法を行う必要があります(IntentServiceがあまりにもContextです):

    Toast.makeText(this, description, Toast.LENGTH_LONG).show(); 
    
  • 関連する問題