Android - Running Service At Boot With No Activity












0















The Problem:



In Android, there does not seem to be a way to specify individual folders within the DCIM folder that you wish to sync to the cloud. The Facebook app saves images to DCIM/Facebook, thus any image that I save is automatically synced to the cloud. I can resolve this by place a .NOMEDIA file within the folder but this now means I can't browse the files without going into a file explorer so I lose easy access to send these images in messages, for instance.



I am working on an Android application at the moment to automatically move files, other than the .NOMDEIA file, from DCIM/Facebook to Pictures/Facebook. I currently have code that will do this in a MainActivity class and I am looking to convert this to a service that will run automatically.



In short, I want this service to start at boot and always run without displaying an activity so it takes away any manual effort on my part. Again, I don't want to have to manually launch anything and I see no need for an activity as I do not want any user interaction. I have read many pages, gotten lost in the documentation, and been to seemingly countless posts concerning Service, IntentService, JobScheduler, etc. and I am currently spinning my wheels on both implementation and correct approach.



Is this a good case for a JobSchedule that runs every few seconds? Should it be a Service that runs the code over and over in a loop that waits a few seconds in each iteration? Is it possible to trigger the service whenever something is added to the folder?



As mentioned above, I've struggled with implementation. At this point, the only thing I've managed to do is call the service from within MainActivity but this happens as the activity briefly opens and closes. This is undesired behavior. Things are getting a bit messy at this point so I've opted not to include any code in hopes that someone can either point me to the right documentation/tutorial or provide a proper skeleton that I can work with. I'd rather not muddy the waters from the start.



Important note: The implementation I have with an activity requests permission for READ_EXTERNAL_STORAGE (it is my understanding that you do not need to request permission for WRITE_EXTERNAL_STORAGE; my code works without requesting that permission) but I am also uncertain how to ensure this is done for a service as the method to request permissions seems to want an activity as one of the parameters.



Edit: Adding manifest and code for receiver and service



FacebookImageListener.java



public class FacebookImageListener extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, FacebookImageMoverService.class));
Log.i("FacebookImageMoverService", "started");
}
}


FacebookImageMoverService.java



public class FacebookImageMoverService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

while (true) {

//TODO remove this
Log.i("imagemover", "DELETE THIS");

// String path = this.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File files = directory.listFiles();

Log.d("Files", "Size: " + files.length);
for (int i = 0; i < files.length; i++) {
Toast toast = Toast.makeText(this.getApplicationContext(), "FileName:" + files[i].getName(), Toast.LENGTH_LONG);
toast.show();
Log.d("Files", "FileName:" + files[i].getName());

if (!files[i].getName().equals(".NOMEDIA")) {
// moveFile(path + "/", files[i].getName(), this.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
moveFile(path + "/", files[i].getName(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
}
}

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

//TODO check this out
// return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}

private void moveFile(String inputPath, String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}


in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);

byte buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file
out.flush();
out.close();
out = null;

// delete the original file
new File(inputPath + inputFile).delete();


} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

}
}


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shogg.facebookimagemover"
>

<!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">

<receiver android:name=".FacebookImageListener"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

<service android:enabled="true" android:name=".FacebookImageMoverService" />
</application>


</manifest>









share|improve this question




















  • 1





    Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

    – Mike M.
    Nov 23 '18 at 4:43
















0















The Problem:



In Android, there does not seem to be a way to specify individual folders within the DCIM folder that you wish to sync to the cloud. The Facebook app saves images to DCIM/Facebook, thus any image that I save is automatically synced to the cloud. I can resolve this by place a .NOMEDIA file within the folder but this now means I can't browse the files without going into a file explorer so I lose easy access to send these images in messages, for instance.



I am working on an Android application at the moment to automatically move files, other than the .NOMDEIA file, from DCIM/Facebook to Pictures/Facebook. I currently have code that will do this in a MainActivity class and I am looking to convert this to a service that will run automatically.



In short, I want this service to start at boot and always run without displaying an activity so it takes away any manual effort on my part. Again, I don't want to have to manually launch anything and I see no need for an activity as I do not want any user interaction. I have read many pages, gotten lost in the documentation, and been to seemingly countless posts concerning Service, IntentService, JobScheduler, etc. and I am currently spinning my wheels on both implementation and correct approach.



Is this a good case for a JobSchedule that runs every few seconds? Should it be a Service that runs the code over and over in a loop that waits a few seconds in each iteration? Is it possible to trigger the service whenever something is added to the folder?



As mentioned above, I've struggled with implementation. At this point, the only thing I've managed to do is call the service from within MainActivity but this happens as the activity briefly opens and closes. This is undesired behavior. Things are getting a bit messy at this point so I've opted not to include any code in hopes that someone can either point me to the right documentation/tutorial or provide a proper skeleton that I can work with. I'd rather not muddy the waters from the start.



Important note: The implementation I have with an activity requests permission for READ_EXTERNAL_STORAGE (it is my understanding that you do not need to request permission for WRITE_EXTERNAL_STORAGE; my code works without requesting that permission) but I am also uncertain how to ensure this is done for a service as the method to request permissions seems to want an activity as one of the parameters.



Edit: Adding manifest and code for receiver and service



FacebookImageListener.java



public class FacebookImageListener extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, FacebookImageMoverService.class));
Log.i("FacebookImageMoverService", "started");
}
}


FacebookImageMoverService.java



public class FacebookImageMoverService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

while (true) {

//TODO remove this
Log.i("imagemover", "DELETE THIS");

// String path = this.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File files = directory.listFiles();

Log.d("Files", "Size: " + files.length);
for (int i = 0; i < files.length; i++) {
Toast toast = Toast.makeText(this.getApplicationContext(), "FileName:" + files[i].getName(), Toast.LENGTH_LONG);
toast.show();
Log.d("Files", "FileName:" + files[i].getName());

if (!files[i].getName().equals(".NOMEDIA")) {
// moveFile(path + "/", files[i].getName(), this.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
moveFile(path + "/", files[i].getName(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
}
}

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

//TODO check this out
// return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}

private void moveFile(String inputPath, String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}


in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);

byte buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file
out.flush();
out.close();
out = null;

// delete the original file
new File(inputPath + inputFile).delete();


} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

}
}


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shogg.facebookimagemover"
>

<!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">

<receiver android:name=".FacebookImageListener"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

<service android:enabled="true" android:name=".FacebookImageMoverService" />
</application>


</manifest>









share|improve this question




















  • 1





    Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

    – Mike M.
    Nov 23 '18 at 4:43














0












0








0








The Problem:



In Android, there does not seem to be a way to specify individual folders within the DCIM folder that you wish to sync to the cloud. The Facebook app saves images to DCIM/Facebook, thus any image that I save is automatically synced to the cloud. I can resolve this by place a .NOMEDIA file within the folder but this now means I can't browse the files without going into a file explorer so I lose easy access to send these images in messages, for instance.



I am working on an Android application at the moment to automatically move files, other than the .NOMDEIA file, from DCIM/Facebook to Pictures/Facebook. I currently have code that will do this in a MainActivity class and I am looking to convert this to a service that will run automatically.



In short, I want this service to start at boot and always run without displaying an activity so it takes away any manual effort on my part. Again, I don't want to have to manually launch anything and I see no need for an activity as I do not want any user interaction. I have read many pages, gotten lost in the documentation, and been to seemingly countless posts concerning Service, IntentService, JobScheduler, etc. and I am currently spinning my wheels on both implementation and correct approach.



Is this a good case for a JobSchedule that runs every few seconds? Should it be a Service that runs the code over and over in a loop that waits a few seconds in each iteration? Is it possible to trigger the service whenever something is added to the folder?



As mentioned above, I've struggled with implementation. At this point, the only thing I've managed to do is call the service from within MainActivity but this happens as the activity briefly opens and closes. This is undesired behavior. Things are getting a bit messy at this point so I've opted not to include any code in hopes that someone can either point me to the right documentation/tutorial or provide a proper skeleton that I can work with. I'd rather not muddy the waters from the start.



Important note: The implementation I have with an activity requests permission for READ_EXTERNAL_STORAGE (it is my understanding that you do not need to request permission for WRITE_EXTERNAL_STORAGE; my code works without requesting that permission) but I am also uncertain how to ensure this is done for a service as the method to request permissions seems to want an activity as one of the parameters.



Edit: Adding manifest and code for receiver and service



FacebookImageListener.java



public class FacebookImageListener extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, FacebookImageMoverService.class));
Log.i("FacebookImageMoverService", "started");
}
}


FacebookImageMoverService.java



public class FacebookImageMoverService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

while (true) {

//TODO remove this
Log.i("imagemover", "DELETE THIS");

// String path = this.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File files = directory.listFiles();

Log.d("Files", "Size: " + files.length);
for (int i = 0; i < files.length; i++) {
Toast toast = Toast.makeText(this.getApplicationContext(), "FileName:" + files[i].getName(), Toast.LENGTH_LONG);
toast.show();
Log.d("Files", "FileName:" + files[i].getName());

if (!files[i].getName().equals(".NOMEDIA")) {
// moveFile(path + "/", files[i].getName(), this.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
moveFile(path + "/", files[i].getName(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
}
}

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

//TODO check this out
// return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}

private void moveFile(String inputPath, String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}


in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);

byte buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file
out.flush();
out.close();
out = null;

// delete the original file
new File(inputPath + inputFile).delete();


} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

}
}


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shogg.facebookimagemover"
>

<!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">

<receiver android:name=".FacebookImageListener"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

<service android:enabled="true" android:name=".FacebookImageMoverService" />
</application>


</manifest>









share|improve this question
















The Problem:



In Android, there does not seem to be a way to specify individual folders within the DCIM folder that you wish to sync to the cloud. The Facebook app saves images to DCIM/Facebook, thus any image that I save is automatically synced to the cloud. I can resolve this by place a .NOMEDIA file within the folder but this now means I can't browse the files without going into a file explorer so I lose easy access to send these images in messages, for instance.



I am working on an Android application at the moment to automatically move files, other than the .NOMDEIA file, from DCIM/Facebook to Pictures/Facebook. I currently have code that will do this in a MainActivity class and I am looking to convert this to a service that will run automatically.



In short, I want this service to start at boot and always run without displaying an activity so it takes away any manual effort on my part. Again, I don't want to have to manually launch anything and I see no need for an activity as I do not want any user interaction. I have read many pages, gotten lost in the documentation, and been to seemingly countless posts concerning Service, IntentService, JobScheduler, etc. and I am currently spinning my wheels on both implementation and correct approach.



Is this a good case for a JobSchedule that runs every few seconds? Should it be a Service that runs the code over and over in a loop that waits a few seconds in each iteration? Is it possible to trigger the service whenever something is added to the folder?



As mentioned above, I've struggled with implementation. At this point, the only thing I've managed to do is call the service from within MainActivity but this happens as the activity briefly opens and closes. This is undesired behavior. Things are getting a bit messy at this point so I've opted not to include any code in hopes that someone can either point me to the right documentation/tutorial or provide a proper skeleton that I can work with. I'd rather not muddy the waters from the start.



Important note: The implementation I have with an activity requests permission for READ_EXTERNAL_STORAGE (it is my understanding that you do not need to request permission for WRITE_EXTERNAL_STORAGE; my code works without requesting that permission) but I am also uncertain how to ensure this is done for a service as the method to request permissions seems to want an activity as one of the parameters.



Edit: Adding manifest and code for receiver and service



FacebookImageListener.java



public class FacebookImageListener extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, FacebookImageMoverService.class));
Log.i("FacebookImageMoverService", "started");
}
}


FacebookImageMoverService.java



public class FacebookImageMoverService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

while (true) {

//TODO remove this
Log.i("imagemover", "DELETE THIS");

// String path = this.getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Facebook";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File files = directory.listFiles();

Log.d("Files", "Size: " + files.length);
for (int i = 0; i < files.length; i++) {
Toast toast = Toast.makeText(this.getApplicationContext(), "FileName:" + files[i].getName(), Toast.LENGTH_LONG);
toast.show();
Log.d("Files", "FileName:" + files[i].getName());

if (!files[i].getName().equals(".NOMEDIA")) {
// moveFile(path + "/", files[i].getName(), this.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
moveFile(path + "/", files[i].getName(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/Facebook/");
}
}

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

//TODO check this out
// return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}

private void moveFile(String inputPath, String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}


in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);

byte buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file
out.flush();
out.close();
out = null;

// delete the original file
new File(inputPath + inputFile).delete();


} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}

}
}


AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shogg.facebookimagemover"
>

<!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">

<receiver android:name=".FacebookImageListener"
android:icon="@mipmap/ic_launcher"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

<service android:enabled="true" android:name=".FacebookImageMoverService" />
</application>


</manifest>






android service android-service android-jobscheduler






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 22:58







Shoggoth269

















asked Nov 22 '18 at 20:12









Shoggoth269Shoggoth269

848




848








  • 1





    Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

    – Mike M.
    Nov 23 '18 at 4:43














  • 1





    Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

    – Mike M.
    Nov 23 '18 at 4:43








1




1





Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

– Mike M.
Nov 23 '18 at 4:43





Since Android 3.1, your app will be installed in a stopped state, and Receivers registered in your app's manifest will not work until your app has been somehow explicitly launched. Basically, you need an Activity that the user can run at least once after installation to bring your app out of that state.

– Mike M.
Nov 23 '18 at 4:43












1 Answer
1






active

oldest

votes


















1














Register a BroadcastReceiver in your AndroidManifest.xml and request the permission to listen for device boot broadcasts by adding this permission to your manifest:



android:permission="android.permission.RECEIVE_BOOT_COMPLETED">



You can then start your service in the onReceive() method of your BroadcastListerner without starting the main activity of your app of obstructing the user in any way.






share|improve this answer
























  • Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

    – Shoggoth269
    Nov 22 '18 at 21:25






  • 1





    Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

    – daedsidog
    Nov 22 '18 at 21:28













  • I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

    – Shoggoth269
    Nov 22 '18 at 23:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437516%2fandroid-running-service-at-boot-with-no-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Register a BroadcastReceiver in your AndroidManifest.xml and request the permission to listen for device boot broadcasts by adding this permission to your manifest:



android:permission="android.permission.RECEIVE_BOOT_COMPLETED">



You can then start your service in the onReceive() method of your BroadcastListerner without starting the main activity of your app of obstructing the user in any way.






share|improve this answer
























  • Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

    – Shoggoth269
    Nov 22 '18 at 21:25






  • 1





    Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

    – daedsidog
    Nov 22 '18 at 21:28













  • I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

    – Shoggoth269
    Nov 22 '18 at 23:01
















1














Register a BroadcastReceiver in your AndroidManifest.xml and request the permission to listen for device boot broadcasts by adding this permission to your manifest:



android:permission="android.permission.RECEIVE_BOOT_COMPLETED">



You can then start your service in the onReceive() method of your BroadcastListerner without starting the main activity of your app of obstructing the user in any way.






share|improve this answer
























  • Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

    – Shoggoth269
    Nov 22 '18 at 21:25






  • 1





    Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

    – daedsidog
    Nov 22 '18 at 21:28













  • I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

    – Shoggoth269
    Nov 22 '18 at 23:01














1












1








1







Register a BroadcastReceiver in your AndroidManifest.xml and request the permission to listen for device boot broadcasts by adding this permission to your manifest:



android:permission="android.permission.RECEIVE_BOOT_COMPLETED">



You can then start your service in the onReceive() method of your BroadcastListerner without starting the main activity of your app of obstructing the user in any way.






share|improve this answer













Register a BroadcastReceiver in your AndroidManifest.xml and request the permission to listen for device boot broadcasts by adding this permission to your manifest:



android:permission="android.permission.RECEIVE_BOOT_COMPLETED">



You can then start your service in the onReceive() method of your BroadcastListerner without starting the main activity of your app of obstructing the user in any way.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 21:12









daedsidogdaedsidog

1,3362828




1,3362828













  • Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

    – Shoggoth269
    Nov 22 '18 at 21:25






  • 1





    Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

    – daedsidog
    Nov 22 '18 at 21:28













  • I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

    – Shoggoth269
    Nov 22 '18 at 23:01



















  • Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

    – Shoggoth269
    Nov 22 '18 at 21:25






  • 1





    Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

    – daedsidog
    Nov 22 '18 at 21:28













  • I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

    – Shoggoth269
    Nov 22 '18 at 23:01

















Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

– Shoggoth269
Nov 22 '18 at 21:25





Thank you for this help. I'll start working on this implementation. Does this address the issue of the service needing permissions to read storage or does that not have to be specified for a service?

– Shoggoth269
Nov 22 '18 at 21:25




1




1





Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

– daedsidog
Nov 22 '18 at 21:28







Depends where you are writing. Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). This taken directly from the reference. If this is not enough, you should still be able to request permission through the service.

– daedsidog
Nov 22 '18 at 21:28















I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

– Shoggoth269
Nov 22 '18 at 23:01





I still seem to be having troubles getting this solution to work. The service does not seem to have executed at all, whether on running from Android Studio or after restarting the device. I've added the code above if this would be of any help. The only thing of note that I'm seeing is: "Waiting for application to come online: shogg.facebookimagemover.test | shogg.facebookimagemover"

– Shoggoth269
Nov 22 '18 at 23:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437516%2fandroid-running-service-at-boot-with-no-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown