如何使用Firebase代码启动前台服务?

时间:2018-03-07 08:54:45

标签: android firebase service firebase-realtime-database

我有一项服务,可以在计时器停止时替换停车位的状态,但只有在我的应用程序运行时才能运行。我想让我的服务取代插槽的状态,即使我的应用程序已关闭。我已经阅读了关于startForeground但我无法找到关于如何正确使用它的明确答案。这是我的代码:

TimerService:

public class TimerService extends Service {


    private Vibrator v;
    private static final int uniqueID = 71399;

    //databae declarations
    private DrawerLayout nDrawerLayout; // variable for DrawerLayout
    private ActionBarDrawerToggle nToggle;// variable for toggle button

    private FirebaseAuth firebaseAuth;//instance of Firebase
    private FirebaseAuth.AuthStateListener authStateListener;
    FirebaseDatabase database;
    DatabaseReference myRef;//database reference

    NotificationCompat.Builder notification;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        notification = new NotificationCompat.Builder(this);
        notification.setAutoCancel(false);

    }


    //starts warning timer
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        SharedPreferences sharedPreferences = getSharedPreferences("Reservation", Context.MODE_PRIVATE);
        String slot = sharedPreferences.getString("slot","");
        Toast.makeText(this,"service slot:"+slot,Toast.LENGTH_LONG).show();
        WarningTimer();
        DurationTimer(slot);
        return START_STICKY;
    }

    //1 hour warning timer
    public void WarningTimer(){
        final long dur = 40000;
        CountDownTimer countDownTimer = new CountDownTimer(dur,1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                long n[] = {1,1000,500,1000,500,1000,500,1000,500,1000};
                v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(n, -1);
                halfnotif();
            }
        };
        countDownTimer.start();

    }

    //full duration timer
    public void DurationTimer(final String slot){
        final long dur2 = 60000;
        CountDownTimer countDownTimer = new CountDownTimer(dur2,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long millis= millisUntilFinished;
                String hms= String.format("%02d:%02d:%02d",

                        TimeUnit.MILLISECONDS.toHours(millis),
                        TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
                        //seconds
                        ,TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
                );

            }

            @Override
            public void onFinish() {
                long n[] = {1,1000,500,1000,500,1000,500,1000,500,1000};
                v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(n, -1);

                //lagay code ng firebase here
                DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("parking_lot");
                myRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            final String key = ds.getKey();
                            //final String[] Car_Slots = {"slot1","slot2","slot3","slot4","slot5","slot6","slot7","slot8","slot9","slot10",
                            //"slot11","slot12","slot13","slot14","slot15","slot16","slot17","slot18","slot19","slot20","slot21","slot22"};
                            DatabaseReference keyReference = FirebaseDatabase.getInstance().getReference().child("parking_lot").child(key);
                            keyReference.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String value = dataSnapshot.child("status").getValue(String.class);
                                    if (key.equals(slot)) {
                                        if (value.equals("occupied")) {
                                            //Toast.makeText(TimerService.this,"occupied (service)"+slot,Toast.LENGTH_LONG).show();onDestroy();
                                        } else if (value.equals("reserved")) {

                                            //Toast.makeText(TimerService.this,"reserved (service)"+slot,Toast.LENGTH_LONG).show();
                                            database = FirebaseDatabase.getInstance();
                                            final DatabaseReference myRef = database.getReference("parking_lot");
                                            final DatabaseReference myRef1 = myRef.child(slot);
                                            final DatabaseReference myRef2 = myRef1.child("status");
                                            myRef2.setValue("vacant");

                                        } else if (value.equals("vacant")) {
                                            //Toast.makeText(TimerService.this,"vacant (service)"+slot,Toast.LENGTH_LONG).show();

                                        }
                                    }

                                    Log.d(TAG, "Read ok");

                                }
                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                    Log.d(TAG, "Read failed");
                                }
                            });
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.d(TAG, "Read failed");
                    }

                });

                endnotif();
            }
        };
        countDownTimer.start();
    }

    @Override
    public void onDestroy() {
        stopSelf();
        super.onDestroy();
    }


    //kalahati
    public void halfnotif(){
        notification.setSmallIcon(R.drawable.ic_info_black_24dp);
        notification.setContentText("1 Hour remaining!");
        notification.setTicker("leave the parking lot!");
        notification.setWhen(System.currentTimeMillis());
        notification.setContentTitle("TICK TOCK");
        //NotificationCompat.Builder builder = notification.setContentText("Apps are now unblocked!");
        ClickNotif();
    }

    //Notify the user that the timer has ended
    public void endnotif(){
        notification.setSmallIcon(R.drawable.ic_directions_car_black_24dp);
        notification.setContentText("00:00");
        notification.setTicker("leave the parking lot!");
        notification.setWhen(System.currentTimeMillis());
        notification.setContentTitle("Times Up!");
        //NotificationCompat.Builder builder = notification.setContentText("Apps are now unblocked!");
        ClickNotif();
    }

    //other parts of  notif
    public void ClickNotif(){
        Intent intent1 = new Intent(this, Parking.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(pendingIntent);
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(uniqueID, notification.build());
        onDestroy();
    }


} 

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

即使您的应用已关闭,要更改广告位的状态,我建议您在Cloud Functions for Firebase中编写一个功能,以便为您执行此更改。

为此,我建议你看看道格的回答post