Unable to start service in Android 6.0 - android

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

Related

do i have to use any foreground service type in manifest if it has no need?

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

Job Scheduler cant find job/service?

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.

How would you use killBackgroundProcesses in Android

Well, hello..
I have a question, how would you correctly use this
"ActivityManager.killBackgroundProcesses(String packageName)"
I have tried to replace packagename with a package, for now let's call it com.example.package.
It doesn't seem to be compiling it as it pops up with errors (I am new to Android app developing)
Would appreciate any help! =)
You must have a special permission to kill background processes from your application. Add the following permission in your manifest.
"uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"
The above permission enables to kill any background process running for the package name provided in "ActivityManager.killBackgroundProcesses(String packageName)"

Android App reports "not installed" due to permission error

On a tablet I installed a apk develloped in Eclipse under Ubuntu. The App works on the AVD and is already installed on a phone and working.
The installation of the apk gives no error, however when starting the App it gives a "not installed" toast message.
In the aLogCat output I see a Permission denial message of the Launcher for WRITE_EXTERNAL_STORAGE. Note that the USB connection is not connected when I started the App.
Furthermore I noticed the following line in aLogCat and I noted that the "-1" was added to the package name.
New package installed in /data/app/com.company.AppName-1.apk
In the manifest the lines
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
are in the manifest block and
<android:permission="android.permission.WRITE_EXTERNAL_STORAGE">
in the application block.
Why is this working on a Android 2.3 phone and in a Android 4.0.3 AVD, but not on a Android 4.0.3 tablet?
Platform info: Eclipse 3.7.2, Tablet: Yarvik TAB461EUK; Installed with "ES File Explorer"
Try removing the permission from the application block. I dont know for sure but from personal experience ICS do not support permission attribute in application block which has already been defined in the main block. It inherits from the main block. Please tell if that solves the problem.
I dont know why it works on the AVD, may be because AVD do not have any external storage.
Related documentation:
http://developer.android.com/guide/topics/manifest/manifest-intro.html describes the permission element only for the main manifest block (now)
Changes per version states:
HONEYCOMB: When an application requires a permission to access one of its components (activity, receiver, service, provider), this permission is no longer enforced when the application wants to access its own component. This means it can require a permission on a component that it does not itself hold and still access that component.
Activity states:
The name of a permission that clients must have to launch the activity or otherwise get it to respond to an intent. If a caller of startActivity() or startActivityForResult() has not been granted the specified permission, its intent will not be delivered to the activity.
If this attribute is not set, the permission set by the element's permission attribute applies to the activity. If neither attribute is set, the activity is not protected by a permission.
Not very clear to me

Web services not running in android project?

I am accessing the JSON webServices but it is not running and it is showing execption.
I made a activity and calling the services from this activity class, when i run from java code services running fine but not running in android....what could be the reason behind this?
I am following standard code of calling from another project, also the services running in that project but not on mine. thanks.
Add permissions to access internet before calling services,
add following lines to your menifest files.
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
check and tell if still there any problem

Categories

Resources