Exemplu de clasă Service bazată pe Backendless pentru Android

Aplicațiile Android îmbunătățesc experiența mobilă pentru utilizatori. Descoperă programarea codului Android. Cere soluții pentru proiectul tău.
Avatar utilizator
specialist it
Mesaje: 113
Membru din: 28 Iun 2017, 16:39
Aprecieri acordate: 4 ✔
Aprecieri primite: 61 ✔
Contact:

Exemplu de clasă Service bazată pe Backendless pentru Android

Mesaj de specialist it » 15 Aug 2017, 13:52

Salut,

Mai jos regăsiți un exemplu de clasă Service în Android care conține metode care vă pot ajuta să interacționați cu Backendless:

Cod: Selectează tot

package com.sch.selfiehistory.common.services;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;

import com.backendless.Backendless;
import com.backendless.BackendlessCollection;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import com.backendless.files.BackendlessFile;
import com.backendless.persistence.BackendlessDataQuery;
import com.backendless.persistence.QueryOptions;
import com.sch.selfiehistory.MainActivity;
import com.sch.selfiehistory.common.Constants;
import com.sch.selfiehistory.common.Session;
import com.sch.selfiehistory.common.models.Selfie;
import com.sch.selfiehistory.common.utils.Alert;
import com.sch.selfiehistory.common.utils.ProgressBarHandler;
import com.sch.selfiehistory.modules.timeline.SelfiesAdapter;
import com.sch.selfiehistory.ui.search_users.UsersCustomList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by CIPRIAN on 11/13/2016.
 */

public class Services {
    public static void getAvatar(final Context context, final BackendlessUser user, final ImageView imageView, final int blankAvatar) {
        String whereClause = "ownerId = '" + user.getUserId() + "'";
        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.addSortByOption("created DESC");
        dataQuery.setQueryOptions(queryOptions);
        dataQuery.setWhereClause(whereClause);

        Backendless.Data.of(Selfie.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Selfie>>() {
        @Override
        public void handleResponse(BackendlessCollection<Selfie> selfies) {
            if (selfies.getCurrentPage().size() > 0) {
                Selfie selfie = selfies.getCurrentPage().get(0);
                Picasso.with(context).load(selfie.getSelfieURL()).into(imageView);
            } else {
                Picasso.with(context).load(blankAvatar).into(imageView);
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void getLastSelfieUpdateAvatar(final Context context, final BackendlessUser user, final ImageView imageView, final int blankAvatar) {
    String whereClause = "ownerId = '" + user.getUserId() + "'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.addSortByOption("created DESC");
    dataQuery.setQueryOptions(queryOptions);
    dataQuery.setWhereClause(whereClause);

    Backendless.Data.of(Selfie.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Selfie>>() {
        @Override
        public void handleResponse(BackendlessCollection<Selfie> selfies) {
            if (selfies.getCurrentPage().size() > 0) {
                Selfie selfie = selfies.getCurrentPage().get(0);
                Picasso.with(context).load(selfie.getSelfieURL()).into(imageView);

                if (!user.getProperty("avatarURL").equals(selfie.getSelfieURL())) {
                    user.setProperty("avatarURL", selfie.getSelfieURL());
                    Backendless.UserService.update(user, new AsyncCallback<BackendlessUser>() {
                        @Override
                        public void handleResponse(BackendlessUser response) {
                            Log.v("Avatar", "Avatar updated !");
                        }

                        @Override
                        public void handleFault(BackendlessFault fault) {
                            Alert.showMessage(context, "Error: " + fault.getMessage());
                        }
                    });
                }

            } else {
                Picasso.with(context).load(blankAvatar).into(imageView);
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void getSelfies(final Context context, final BackendlessUser user, final SelfiesAdapter adapter, final List<Selfie> selfieList) {
    String whereClause = "ownerId = '" + user.getUserId() + "'";

    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    QueryOptions queryOptions = new QueryOptions();
    //queryOptions.setPageSize(25);
    //queryOptions.setOffset(50);
    queryOptions.addSortByOption("created DESC");
    dataQuery.setQueryOptions(queryOptions);
    dataQuery.setWhereClause(whereClause);

    Backendless.Data.of(Selfie.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Selfie>>() {
        @Override
        public void handleResponse(BackendlessCollection<Selfie> selfies) {
            List<Selfie> firstpage = selfies.getCurrentPage();
            Iterator<Selfie> iterator = firstpage.iterator();

            while (iterator.hasNext()) {
                Selfie selfie = iterator.next();
                selfieList.add(selfie);
            }

            adapter.notifyDataSetChanged();
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void logIn(final Context context, final BackendlessUser user, final Class goTo) {
    Backendless.UserService.login(user.getEmail(), user.getPassword(), new AsyncCallback<BackendlessUser>() {
        @Override
        public void handleResponse(BackendlessUser user) {
            Session.get().setUser(user);
            Intent intent = new Intent(context, goTo);
            context.startActivity(intent);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    }, true);
}

public static void logOut(final Context context, final Class goTo) {
    Backendless.UserService.logout(new AsyncCallback<Void>() {
        @Override
        public void handleResponse(Void response) {
            Intent intent = new Intent(context, goTo);
            context.startActivity(intent);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void logUser(final Context context, String userId, final Class goTo) {
    Backendless.UserService.findById(userId, new AsyncCallback<BackendlessUser>() {
        @Override
        public void handleResponse(BackendlessUser user) {
            if (user != null) {
                Backendless.UserService.setCurrentUser(user);
                Session.get().setUser(user);
                Intent intent = new Intent(context, goTo);
                context.startActivity(intent);
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void photoUpload(final Context context, final Bitmap bitmap, final String selfieName, final BackendlessUser user) {
    Backendless.Files.Android.upload(bitmap, Bitmap.CompressFormat.PNG, 100, selfieName, Constants.BACKENDLESS_SELFIES_PATH, new AsyncCallback<BackendlessFile>() {
        @Override
        public void handleResponse(BackendlessFile response) {
            final HashMap selfie = new HashMap();
            selfie.put("selfieName", selfieName);
            selfie.put("selfieOwnerName", user.getProperty("name"));
            selfie.put("selfieURL", response.getFileURL());

            Services.saveSelfie(context, selfie);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void register(final Context context, final BackendlessUser user, Class goTo) {
    Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
        @Override
        public void handleResponse(BackendlessUser response) {
            Alert.showMessage(context, "Registration " + response.getEmail() + " successfully registered");
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void saveSelfie(final Context context, final HashMap selfie) {
    Backendless.Persistence.of("Selfie").save(selfie, new AsyncCallback<Map>() {
        @Override
        public void handleResponse(Map response) {
            Alert.showMessage(context, "Selfie posted: " + selfie.get("selfieName"));
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}

public static void searchUsers(final Context context, final Activity activity, final ListView listView, final String search, final ProgressBarHandler progressBarHandler) {
    final ArrayList<String> userNames = new ArrayList<String>();

    progressBarHandler.show();

    String whereClause = "name LIKE '%" + search + "%'";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.addSortByOption("created DESC");
    dataQuery.setQueryOptions(queryOptions);
    dataQuery.setWhereClause(whereClause);

    Backendless.Data.of(BackendlessUser.class).find(dataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
        @Override
        public void handleResponse(BackendlessCollection<BackendlessUser> response) {
            ArrayList<BackendlessUser> firstpage = (ArrayList<BackendlessUser>) response.getCurrentPage();
            Iterator<BackendlessUser> iterator = firstpage.iterator();

            while (iterator.hasNext()) {
                BackendlessUser user = iterator.next();
                userNames.add(user.getProperty("name").toString());
            }

            UsersCustomList adapter = new UsersCustomList(activity, userNames, firstpage);
            listView.setAdapter(adapter);

            Session.get().setSearchUsers(firstpage);

            progressBarHandler.hide();
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Alert.showMessage(context, "Error: " + fault.getMessage());
        }
    });
}
}
Pentru întrebări, vă stăm la dispoziție.

Autor thread: stefanciprian, Echipa Specialişti IT

Mulțumesc


Moderator Unic - Forum Specialistul IT

Scrie răspuns

Utilizatori online

Utilizatori care navighează pe acest forum: Nici un utilizator înregistrat și 1 vizitator

Subiecte active