Android job scheduler not persisted on reboot - android

I have implemented Job scheduler in my project and it works fine in the case if the app is in background or if the app is killed. But it is not working if the device is rebooted. I have included
JobInfo.Builder mBuilder = new JobInfo.Builder(TASK_ID, mComponentName);
mBuilder.setPersisted(true);
in my builder and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in application manifest file.This is how I have added my service to manifest
<service
android:name="com.xxx.xxxx.service.MyJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
Is there anything else to be included?
Thanks in advance

Register a BroadCastReciever for detecting BOOT_COMPLETED.
<receiver android:name="com.example.startuptest.StartUpBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
And in your BroadcastReceiver:
public class StartUpBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d("startuptest", "StartUpBootReceiver BOOT_COMPLETED");
...
}
}
}
Once a user runs any activity in your app once, you will receive the BOOT_COMPLETED broadcast after all future boots.

I know it is an old question, and you probably already have the solution, but the issue likely was, that JobService caches the UUIDs of the apps that have the permission. You will need to reinstall your app and it is gonna be alright.
Source:
https://android.googlesource.com/platform/frameworks/base/+/5d34605/services/core/java/com/android/server/job/JobSchedulerService.java

You need to call the setPersisted(boolean isPersisted) method.
You can find it in the doc https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setPersisted(boolean)

Related

How to start an application directly (without asking) at startup on Android 4.0

I want to start my application at startup in Android 4.0. To do that, I wrote some codes and these are completely the same with the #Ahmad's codes (in the answer). However, although I select my application as always, when tablet opens, it asks 'What do you prefer?' (Android's default launcher or my application). I don't want it to ask that question and it must start my application automatically.
Use the BOOT_COMPLETED Intent.
Broadcast Action: This is broadcast once, after the system has
finished booting. It can be used to perform application-specific
initialization, such as installing alarms. You must hold the
RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
In your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Set up a Broadcastreceiver:
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is how your BroadcastReceiver could look like:
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
startActivity(i);
}
}

How to start the application after boot complete in android version 3.1 and above?

I got to know boot complete intent is not supported by android version 3.1 and above from here. But in my application. I want to start services automatically in my application after device is rebooted. I do not want user to start my application manually. How can I do it ? Thanks for help in advance.
What makes you think the ACTION_BOOT_COMPLETED broadcast is no longer sent? I make frequent use of it, and so do others. Just be sure you have RECEIVE_BOOT_COMPLETED permission in your manifest.
try the following steps to start the application after boot:
create a class which extends BroadcastReceiver:
public class AutostartService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("in broad....");
if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
{
System.out.println("in broadcast receiver.....");
Intent i = new Intent(context, Splash.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
and also add this in android manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".AutostartService" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Well this is pretty old now and probably most people learned it but still might be useful.
From 3.1+ and on, in order to receive the BOOT_COMPLETED, your app must be started at least once by the user.
NOTE: This rule doesn't apply to /system apps

Restore Alarm manager after phone reboot

i am building a small widget for learning purpose, it simply has an configuration activity where i set the update interval. it works normally and i can create multiple instance of it.
but when i reboot the phone the alarm manager stops, and the widget won't update.
after some search and google'ng i learned that i have to add a BOOT COMPLETE receiver
but after several attempts i failed to implement so any one has an i idea about how to add that or any good source code example on widgets.
To do something at boot you simply do following.
First in the manifest, this is added under application tag:
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="packagename.ACTION"/>
<action android:name="packagename.ACTION2"/>
</intent-filter>
</receiver>
<receiver android:name="BootSetter" >
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In order for this to work you need to add permission to receive the Broadcast in the manifest with following line:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then you have a class BootSetter:
public class BootSetter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do your stuff
}
}
There is a similar post, though not completly the same here. It's about running an alarm every day at noon.
I think you are setting alarm manager in class other then AppWidgetProvider extended class(widget class) .Better you should set an alarmmanager in OnUpdate method AppWidgetProvider extended class (widget class)then there will be no need of setting the alarm again after boot.

Start Alarm on Android without having the application running

My idea is to set an alarm for a specific date in my application, but I want to be able to have the alarm ringing at the set date, even if my application isn't running at all.
How can I achieve this?
Thanks in advance!
I'd start a service when the device is booted - that service should take care about the alarming when the time has come.
To make your service be started at boot time you need the following things in your AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in the <manifest> tag
<receiver android:name="com.yourpackage.AlarmingBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
in your <application> tag
Additionally you need your AlarmingBroadcastReceiver, should look something like that to start the service:
public class AlarmingBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, AlarmingService.class);
context.startService(startServiceIntent);
}
}
whereas AlarmingService.class is the class name of your service that finally takes care about the alarming stuff
You will need to create a onBoot BroadCast Receiver so when the device is started your application will get control to set up alarms.

Detect USB in Broadcastreceiver - what am I missing?

My code misses something, need your eyes to locate.
I created a USBOnReciever broadcastreceiver.
public class USBOnReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was connected to power" ,Toast.LENGTH_LONG).show();
Log.d("tag", "Phone was connected to power");
}
}
My manifest is:
<application>
<receiver android:name=".USBOnReceiver" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
</intent-filter>
</receiver>
</application>
and here is when I stuck.
nothing appears to happen when connecting the USB ( I also tried with power_connected).
I understand I can register the BR either through the manifest or programmaticaly. But not sure how to implement.
In my activity I added
USBOnReceiver myReceiver = new USBOnReceiver();
But it looks so unconnected and useless :-/
Will appreciate your eyes here.
Your code is seems complete. If im not mistaken,
the only issue you have is: You wrote reciever inside your manifest instead of receiver.
So the BroadcastReceiver never got registered correctly.
To the topic of registering in code:
Yeah thats possible. Just create an instance of your receiver
and use Context.registerReceiver(mReceiver, new IntentFilter(...)).
If you have registered in your manifest, there is no need to do that.
BroadcastReceivers only live for the execution of onReceive(). Therefore
the system creates the instances and kills them afterwards.

Categories

Resources