My app needs to do something periodically so I'm trying the Job Scheduler introduced in Android API 21. At this moment I'm just gettingto know it and try the basics. Unfortunately I get an error:
Java.lang.illegalArgumentException: No such service ComponentInfo{The whole path here to the service class name TestJobService}
I understand this has been solved for some by adding permission to Manifest, but not me. What else can cause this?
This is my code part where I guess it goes wrong:
Toast.makeText(getApplicationContext(), "Exec reaches here...",Toast.LENGTH_SHORT).show();
JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(),TestJobService.class.getName()));
//runs job service after every 10 seconds
builder.setPeriodic(10000);
jobScheduler.schedule(builder.build());
I have this in my Manifest:
<service
android:name=".TestJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true" >
</service>
One thing that confuses me is that I cant manually give job-permission to the app manually in the device settings (I do that to get permission to storage). If the app needs permission for schedualing jobs, shouldn't I have to give permission in the settings?
I really hope someone has the solution for this.
Thanks.
Make sure you put your service element inside application element and nowhere else. When I did it worked fine.
add <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> to the user permissions in the manifest. it did it for me.
Related
I am working on scheduling app in which i am using foreground service , in below code i am using dataSync but there is no need of it on my project , i saw many developers are using at least 1 foreground service type thats why i have mentioned it , my question is if i removed it would it effect my app or it is ok
Here is my code in manifest
<service android:name=".service.CountdownService"
android:foregroundServiceType="dataSync"
/>
If your service is not using anything like camera, microphone and location then attribute foregroundServiceType has no usage.
You can check this documentation.
https://developer.android.com/guide/components/foreground-services#loc-camera
I have created a JobIntentSerivce and in the manifest file for the MyService class I didn't specify the BIND_JOB_SERVICE permission. So while I enqueue work for this service it is throwing run time error "MyService does not require android.permission.BIND_JOB_SERVICE permission". If I give the permission to MyService in android manifest it will fix the issue. But my doubt is why the error showing "does not require permission", is it a typo or any logical reason behind it?
JobIntentService must be protected by BIND_JOB_SERVICE permission, but while adding permission in the manifest, setting android:exported to true will resolve this somewhat confusing error e.g
<service android:name="com.example.jobscheduler.MyService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
Looking at the source the comments on the method: enforceValidJobRequest(int uid, JobInfo job) in JobSchedulerStub does go some way to explain what it is checking :
JobSchedulerService.JobSchedulerStub :
// Enforce that only the app itself (or shared uid participant) can schedule a
// job that runs one of the app's services, as well as verifying that the
// named service properly requires the BIND_JOB_SERVICE permission
private void enforceValidJobRequest(int uid, JobInfo job) { .. }
Inside this method it definitely checks that the Service in JobInfo (jobinfo.getService()) has the permission BIND_JOB_SERVICE - if not throws the error, to me a somewhat misleading message. It Also checks that only the system or app uid can enqueue work, this is obviously for security reasons.
Im using GcmTaskService for sending data in the background, In most cases it works well, altough lately i got one complaint from a client that data is not being sent out of the device. Ive brought the device and I notice that my GcmTaskService's onRunTask never being called on this specific device.
This is how i initiate the service :
GcmNetworkManager gcmNetworkManager = GcmNetworkManager.getInstance(FarmWorkApplication.context);
Class<FarmWorkSyncOutService> gcmTaskService = FarmWorkSyncOutService.class;
String name = gcmTaskService.getName();
gcmNetworkManager.cancelTask(name, gcmTaskService);
OneoffTask task = new OneoffTask.Builder()
.setService(gcmTaskService)
.setTag(name)
.setExecutionWindow(0L, maxDelay)
.setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
.build();
gcmNetworkManager.schedule(task);
This is the service definition in the manifest :
<service android:name=".FarmWorkSyncOutService"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
</intent-filter>
</service>
The device is connected to the internet by WIFI and is surfing the internet very well.
This line :
gcmNetworkManager.cancelTask(name, gcmTaskService);
is just to assure the service runs once, i also tried to remove it and it didnt help
I tried force stopping GooglePlayServices and remove its data, it didnt help what else can i do to debug this? any1 got any idea why this could happen?
This was the Power Saving Mode's fault. once i disabled it the device started syncing out. wonder if that was smart of them to make Services never run in the background at all...
I'm trying to run a service by using following code:
Intent i = new Intent(getApplicationContext(),MyService.class);
startService(i);
This code works fine on versions older than Android 6.0 Marshmallow. But here on Android 6.0, the service never gets created. startService() is returning null. I'm calling it from an Activity in the same package. My manifest is like:
<service
android:name="com.example.app2.MyService"
android:label="#string/app_name">
</service>
Also my application is not crashing, it's just the service is not starting (not created).
Once I was having the same problem. It was a very small and stupid mistake. I had given <service> tag outside <application> tag in manifest.xml. After moving it inside <application> tag service started successfully. Check for any typos in service name also. Hope it helps!
If you are accessing any device hardware features(Camera, Storage, Location etc) in the Service. You need to ask runtime permission in the Marshmallow devices. Otherwise, it will make a crash if your compile SDK version >=23. Please paste your Crash log or Service code for further clarification.
Please check the link to implement the feature:
https://developer.android.com/training/permissions/index.html
https://developer.android.com/training/permissions/requesting.html
I'm just playing around with some code. I create an Activity and simply do something like this:
long lo = currentTimeMillis();
System.out.println(lo);
lo *= 3;
System.out.println(lo);
SystemClock.setCurrentTimeMillis(lo);
System.out.println( currentTimeMillis() );
Yes, in my AndroidManifest.xml, I've added:
<uses-permission android:name="android.permission.SET_TIME"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>
Nothing changes. The SystemClock is never reset...it just keeps on ticking.
The error that I'm getting just says that the permission "SET_TIME" was not granted to the program. Protection level 3.
The permissions are there...and in the API for 2.2 it says that this feature is supported now. I have no idea what I'm doing wrong.
If android.content.Intent; comes into play, please explain. I don't really understand what the idea behind intents!
Thanks for any help!
There is a SET_TIME_ZONE permission but there's no SET_TIME permission. Applications cannot programmatically change the system clock.
Update
SET_TIME is available since 2.2, but can only be granted to the system process or apps signed with the system signature.
using AlarmManager with SET_TIME permission to set system time seems to work :)
insetad of System.out.println() use Log.v() or similar.
I think I found your error, please try it out: Remove
</uses-permission>
on both lines, that should work