|
|
|
สอบถามเกี่ยวกับการเขียน Android Geofencing ครับ รันได้ ไม่มี Error แต่ไม่มีการแจ้งเตือนใดๆ |
|
|
|
|
|
|
|
ผมลองเขียนโค้ด geofence ในโปรแกรม Android Studio สามารถรันได้ ไม่มี Error แต่ไม่มี Notification แจ้งเตือนเลย ทั้งเวลาเดินเข้า ขณะอยู่ และเดินออกจากพิกัด รบกวนทีครับ
MainActivity
Code (Android-Java)
package com.example.crmtracking.fromweb;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class MainActivity extends FragmentActivity implements OnCameraChangeListener {
private GoogleMap mMap;
ArrayList<Geofence> mGeofences;
ArrayList<LatLng> mGeofenceCoordinates;
ArrayList<Integer> mGeofenceRadius;
private GeofenceStore mGeofenceStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions().position(
new LatLng(13.729468, 100.779361))
.title("คณะวิทยาศาสตร์"));
mMap.addMarker(new MarkerOptions().position(
new LatLng(13.726566, 100.780104))
.title("คณะเทคโนโลยีการเกษตร"));
mMap.addMarker(new MarkerOptions().position(
new LatLng(13.725534, 100.776338))
.title("คณะสถาปัตยกรรมศาสตร์"));
mMap.addMarker(new MarkerOptions().position(
new LatLng(13.727108, 100.773323))
.title("คณะวิศวะกรรมศาสตร์"));
mGeofences = new ArrayList<Geofence>();
mGeofenceCoordinates = new ArrayList<LatLng>();
mGeofenceRadius = new ArrayList<Integer>();
mGeofenceCoordinates.add(new LatLng(13.729468, 100.779361)); //วิทยาศาสตร์
mGeofenceCoordinates.add(new LatLng(13.726566, 100.780104)); //เทคโนโลยีการเกษตร
mGeofenceCoordinates.add(new LatLng(13.725534, 100.776338)); //สถาปัตยกรรมศาสตร์
mGeofenceCoordinates.add(new LatLng(13.727108, 100.773323)); //วิศวะกรรมศาสตร์
mGeofenceRadius.add(120);
mGeofenceRadius.add(120);
mGeofenceRadius.add(120);
mGeofenceRadius.add(120);
// วิทยาศาสตร์
mGeofences.add(new Geofence.Builder()
.setRequestId("คณะวิทยาศาสตร์")
.setCircularRegion(mGeofenceCoordinates.get(0).latitude, mGeofenceCoordinates.get(0).longitude, mGeofenceRadius.get(0).intValue())
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setLoiteringDelay(30000)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
// เทคโนโลยีการเกษตร
mGeofences.add(new Geofence.Builder()
.setRequestId("คณะเทคโนโลยีการเกษตร")
.setCircularRegion(mGeofenceCoordinates.get(1).latitude, mGeofenceCoordinates.get(1).longitude, mGeofenceRadius.get(1).intValue())
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setLoiteringDelay(30000)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
// สถาปัตยกรรมศาสตร์
mGeofences.add(new Geofence.Builder()
.setRequestId("คณะสถาปัตยกรรมศาสตร์")
.setCircularRegion(mGeofenceCoordinates.get(2).latitude, mGeofenceCoordinates.get(2).longitude, mGeofenceRadius.get(2).intValue())
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setLoiteringDelay(30000)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
// วิศวะกรรมศาสตร์
mGeofences.add(new Geofence.Builder()
.setRequestId("คณะวิศวะกรรมศาสตร์")
.setCircularRegion(mGeofenceCoordinates.get(3).latitude, mGeofenceCoordinates.get(3).longitude, mGeofenceRadius.get(3).intValue())
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setLoiteringDelay(30000)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_DWELL
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
mGeofenceStore = new GeofenceStore(this, mGeofences);
btnShow = (Button) findViewById(R.id.Bsearch);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
mGeofenceStore.disconnect();
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
setUpMapIfNeeded();
} else {
GooglePlayServicesUtil.getErrorDialog(
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
this, 0);
}
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
public void Onclick(View v) {
Context g1 = getApplicationContext();
String g2 = "คุณอยู่ในคณะวิทยาศาสตร์";
int g3 = Toast.LENGTH_LONG;
Toast g = Toast.makeText(g1, g2, g3);
Context t1 = getApplicationContext();
String t2 = "คุณไม่ได้อยู่ในคณะวิทยาศาสตร์";
int t3 = Toast.LENGTH_LONG;
Toast t = Toast.makeText(t1, t2, t3);
Context a1 = getApplicationContext();
String a2 = "คุณอยู่ในคณะเทคโนโลยีการเกษตร";
int a3 = Toast.LENGTH_LONG;
Toast a = Toast.makeText(a1, a2, a3);
Context b1 = getApplicationContext();
String b2 = "คุณไม่ได้อยู่ในคณะเทคโนโลยีการเกษตร";
int b3 = Toast.LENGTH_LONG;
Toast b = Toast.makeText(b1, b2, b3);
Context c1 = getApplicationContext();
String c2 = "คุณอยู่ในคณะสถาปัตยกรรมศาสตร์";
int c3 = Toast.LENGTH_LONG;
Toast c = Toast.makeText(c1, c2, c3);
Context d1 = getApplicationContext();
String d2 = "คุณไม่ได้อยู่ในคณะสถาปัตยกรรมศาสตร์";
int d3 = Toast.LENGTH_LONG;
Toast d = Toast.makeText(d1, d2, d3);
Context e1 = getApplicationContext();
String e2 = "คุณอยู่ในคณะวิศวะกรรมศาสตร์";
int e3 = Toast.LENGTH_LONG;
Toast e = Toast.makeText(e1, e2, e3);
Context f1 = getApplicationContext();
String f2 = "คุณไม่ได้อยู่ในคณะวิศวะกรรมศาสตร์";
int f3 = Toast.LENGTH_LONG;
Toast f = Toast.makeText(f1, f2, f3);
}
private void setUpMap() {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.729468, 100.779361), 16));
mMap.setIndoorEnabled(false);
mMap.setMyLocationEnabled(true);
mMap.setOnCameraChangeListener(this);
}
@Override
public void onCameraChange(CameraPosition position) {
for(int i = 0; i < mGeofenceCoordinates.size(); i++)
{
mMap.addCircle(new CircleOptions()
.center(mGeofenceCoordinates.get(i))
.radius(mGeofenceRadius.get(i).intValue())
.fillColor(0x40ff0000)
.strokeColor(Color.TRANSPARENT)
.strokeWidth(2));
}
}
}
GeofenceStore
Code (Android-Java)
package com.example.crmtracking.fromweb;
import java.util.ArrayList;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class GeofenceStore implements ConnectionCallbacks,
OnConnectionFailedListener, ResultCallback<Status>, LocationListener {
private final String TAG = this.getClass().getSimpleName();
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private PendingIntent mPendingIntent;
private ArrayList<Geofence> mGeofences;
private GeofencingRequest mGeofencingRequest;
private LocationRequest mLocationRequest;
public GeofenceStore(Context context, ArrayList<Geofence> geofences) {
mContext = context;
mGeofences = new ArrayList<Geofence>(geofences);
mPendingIntent = null;
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mGoogleApiClient.connect();
}
@Override
public void onResult(Status result) {
if (result.isSuccess()) {
Log.v(TAG, "Success!");
} else if (result.hasResolution()) {
// TODO Handle resolution
} else if (result.isCanceled()) {
Log.v(TAG, "Canceled");
} else if (result.isInterrupted()) {
Log.v(TAG, "Interrupted");
} else {
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v(TAG, "Connection failed.");
}
@Override
public void onConnected(Bundle connectionHint) {
mGeofencingRequest = new GeofencingRequest.Builder().addGeofences(
mGeofences).build();
mPendingIntent = createRequestPendingIntent();
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
PendingResult<Status> pendingResult = LocationServices.GeofencingApi
.addGeofences(mGoogleApiClient, mGeofencingRequest,
mPendingIntent);
pendingResult.setResultCallback(this);
}
@Override
public void onConnectionSuspended(int cause) {
Log.v(TAG, "Connection suspended.");
}
private PendingIntent createRequestPendingIntent() {
if (mPendingIntent == null) {
Log.v(TAG, "Creating PendingIntent");
Intent intent = new Intent(mContext, GeofenceIntentService.class);
mPendingIntent = PendingIntent.getService(mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
return mPendingIntent;
}
@Override
public void onLocationChanged(Location location) {
Log.v(TAG, "Location Information\n"
+ "==========\n"
+ "Provider:\t" + location.getProvider() + "\n"
+ "Lat & Long:\t" + location.getLatitude() + ", "
+ location.getLongitude() + "\n"
+ "Altitude:\t" + location.getAltitude() + "\n"
+ "Bearing:\t" + location.getBearing() + "\n"
+ "Speed:\t\t" + location.getSpeed() + "\n"
+ "Accuracy:\t" + location.getAccuracy() + "\n");
}
public void disconnect() {
mGoogleApiClient.disconnect();
}
}
GeofenceIntentService
Code (Android-Java)
package com.example.crmtracking.fromweb;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import java.util.List;
public class GeofenceIntentService extends IntentService {
private final String TAG = this.getClass().getCanonicalName();
public GeofenceIntentService() {
super("GeofenceIntentService");
Log.v(TAG, "Constructor.");
}
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
}
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
}
@Override
protected void onHandleIntent(Intent intent) {
Context not = getApplicationContext();
String nnn = "คุณเดินออกจากคณะ";
int durater = Toast.LENGTH_LONG;
Toast t = Toast.makeText(not, nnn, durater);
Context inn = getApplicationContext();
String kkk = "คุณเดินเข้าคณะ";
int rrr = Toast.LENGTH_LONG;
Toast g = Toast.makeText(inn, kkk, rrr);
Context zn = getApplicationContext();
String zz = "คุณอยู่ในคณะ";
int zzz = Toast.LENGTH_LONG;
Toast z = Toast.makeText(zn, zz, zzz);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
Log.v(TAG, "onHandleIntent");
if(!geofencingEvent.hasError()) {
int transition = geofencingEvent.getGeofenceTransition();
String notificationTitle;
switch(transition) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
notificationTitle = "Geofence Entered";
Log.v(TAG, "Geofence Entered");
g.show();
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
notificationTitle = "Geofence Dwell";
Log.v(TAG, "Dwelling in Geofence");
z.show();
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
notificationTitle = "Geofence Exit";
Log.v(TAG, "Geofence Exited");
t.show();
break;
default:
notificationTitle = "Geofence Unknown";
}
sendNotification(this, getTriggeringGeofences(intent), notificationTitle);
}
}
private void sendNotification(Context context, String notificationText,
String notificationTitle) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1000, notificationBuilder.build());
wakeLock.release();
}
private String getTriggeringGeofences(Intent intent) {
GeofencingEvent geofenceEvent = GeofencingEvent.fromIntent(intent);
List<Geofence> geofences = geofenceEvent
.getTriggeringGeofences();
String[] geofenceIds = new String[geofences.size()];
for (int i = 0; i < geofences.size(); i++) {
geofenceIds[i] = geofences.get(i).getRequestId();
}
return TextUtils.join(", ", geofenceIds);
}
}
Tag : Mobile, Android, JAVA, Mobile
|
|
|
|
|
|
Date :
2016-02-14 17:34:45 |
By :
ithikom5004 |
View :
1397 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ลอง Debug ดูครับ ว่ามันทำงานในส่วนควรที่จะทำหรือไม่
|
|
|
|
|
Date :
2016-02-17 10:09:01 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|