Мобильный клиент Azure в Broadcast Receiver android Studio - PullRequest
0 голосов
/ 09 февраля 2019

Как я могу подключить базу данных Azure от приемника вещания в Android, например, для использования клиента мобильного сервиса?Я опубликовал следующий код широковещательного приемника.

Я использую широковещательный приемник для триггера геозоны, и мне нужно вставить данные в базу данных, когда триггеры геозон входят или выходят

public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "LUBroadcastReceiver";

    static final String ACTION_PROCESS_UPDATES =
            "snmn.sportsconnectappandroid.action" +
                    ".PROCESS_UPDATES";
    public static ArrayList<String> currentlocation_pinpoint_array = new ArrayList<>();
    public static final int GEOFENCE_NOTIFICATION_ID = 0;
    String Locationid,CurrentUserid;
    LocationResultHelper locationResultHelper;
    @Override
    public void onReceive(Context context, Intent intent) {
        android.os.Debug.waitForDebugger();
        if (intent != null) {
            GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
            if (geofencingEvent.hasError()) {
                String errorMsg = getErrorString(geofencingEvent.getErrorCode());
                Log.e(TAG, errorMsg);
                return;
            }
            int geoFenceTransition = geofencingEvent.getGeofenceTransition();
            if(geoFenceTransition > 0) {
                MobileServiceClient mobileServiceClient = null;
                SharedPreferences prefs = context.getSharedPreferences("userid", Context.MODE_PRIVATE);
                CurrentUserid = prefs.getString("currentuserid", "No name defined");//"No name defined" is the default value.
                Log.e("sharedpref_currentuserid",CurrentUserid);
                if(AzureServiceAdapter.mClient==null){
                    try {
                        mobileServiceClient  = new MobileServiceClient("https://sportsconnectapp.azurewebsites.net", context);
                        Log.e("mclient", "new");
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
                else{
                    mobileServiceClient = AzureServiceAdapter.mClient;
                    Log.e("mclient", "ASD");
                }
                mobileServiceClient.setAndroidHttpClientFactory(new OkHttpClientFactory() {
                    @Override
                    public OkHttpClient createOkHttpClient() {
                        OkHttpClient client = new OkHttpClient();
                        client.setReadTimeout(20, TimeUnit.SECONDS);
                        client.setWriteTimeout(20, TimeUnit.SECONDS);
                        return client;
                    }
                });
                MobileServiceTable<LocationUser> mLocationUser = mobileServiceClient.getTable("LocationUser",LocationUser.class);

                if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                    List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
                    String geofenceTransitionDetails = getGeofenceTransitionDetails(geoFenceTransition, triggeringGeofences);
                    for (Geofence geofence : triggeringGeofences) {
                        Locationid = geofence.getRequestId();
                    }
                    Log.e("triggered_geofence", Locationid);
                    locationResultHelper = new LocationResultHelper(context);
                    locationResultHelper.showNotification(geofenceTransitionDetails);
//                    Intent intent1 = new Intent( context,MapsActivity.class);
//                    intent1.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
//                    intent1.setAction("mac.baseline.baselinemobile.BroadcastReceiver");
//                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent1);

                    // APICALLFOR_DATA_STORE(geofenceTransitionDetails,mLocationUser);

                }
            }

//            if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
//                Log.e("exitting_geofence",Locationid);
//                List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
//                String geofenceTransitionDetails = getGeofenceTransitionDetails(geoFenceTransition, triggeringGeofences);
//                locationResultHelper = new LocationResultHelper(context);
//                locationResultHelper.showNotification(geofenceTransitionDetails);
//                APICALLFOR_EXITLOCATION(geofenceTransitionDetails);
//            }

        }
    }
    private String getGeofenceTransitionDetails(int geoFenceTransition, List<Geofence> triggeringGeofences) {
        // get the ID of each geofence triggered
        ArrayList<String> triggeringGeofencesList = new ArrayList<>();
        for (Geofence geofence : triggeringGeofences) {
            triggeringGeofencesList.add(" SportsConnect Location");
             Locationid = geofence.getRequestId();
        }
        String status = null;
        if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER)
            status = "Entering";

        if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT)
            status = "Exiting ";
        return status + TextUtils.join(", ", triggeringGeofencesList);
    }
    private static String getErrorString(int errorCode) {
        switch (errorCode) {
            case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE:
                return "GeoFence Location not available";
            case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES:
                return "Too many GeoFence Locations";
            case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS:
                return "Too many pending intents";
            default:
                return "Unknown error.";
        }
    }

    private void APICALLFOR_DATA_STORE(final String geofenceTransitionDetails, final MobileServiceTable<LocationUser> mloc){
        //store data

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {

                try {
                   // Log.e("UserID", userID);

                    getlocationuserdata(mloc);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                    Log.e("api1",e.getMessage());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e("api2",e.getMessage());
                } catch (MobileServiceException e) {
                    e.printStackTrace();
                    Log.e("api3",e.getMessage());
                }
                //  GeofenceTransitionService geofenceTransitionService = new GeofenceTransitionService();
                   // geofenceTransitionService.APICALLFOR_DATA_STORE(geofenceTransitionDetails);
                    locationResultHelper.showNotification(geofenceTransitionDetails);
                   // Store_Geofence_triggered_data();


                return null;
            }
        };
        runAsyncTask(task);
    }
    public void getlocationuserdata(MobileServiceTable<LocationUser> mloc) throws ExecutionException, InterruptedException, MobileServiceException {
        Log.e("gettinglocationuserdata","No");
        final MobileServiceList<LocationUser> locationuserdata = mloc.execute().get();
        Log.e("gettinglocationuserdata","yes");
}
    private AsyncTask<Void, Void, Void> runAsyncTask(AsyncTask<Void, Void, Void> task) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            return task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            return task.execute();
        }
    }

//    private void APICALLFOR_EXITLOCATION(final String geofenceTransitionDetails){
}

}
...