Unable to start service Intent - android

I've read probably 100 questions and answers on this issue, but I cant seem to get this working. I'm trying to start a Service from an Activity. My manifest file seems OK, the way I'm starting the Service also seems to be correct. The following error is showing up in LogCat:
ActivityManager(1296): Unable to start service Intent
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found
I'm attempting to start the service by calling this in my Activity:
startService(new Intent(getApplicationContext(), Communication.class));
The Service is the following:
public class Communication extends Service {
public Communication() {
super();
}
#Override
public void onCreate() {
super.onCreate();
Log.i("Service", "Created service");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service", "onStartCommand called");
return START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
The entry in my manifest file is:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidClient" android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/sms" android:label="#string/app_name" >
<activity> ... </activity>
<service android:enabled="true" android:name=".Communication" />
</application>
</manifest>
Any advice is greatly appriciated.

Where is the Communication class?
In your manifest you declare a service with android:name=".Communication", this means that your service class should be located in com.exercise.AndroidClient.Communication
Check that the packages are correct. Note that the "." (dot) refers to the root of your package (ie the package declared in the manifest). So, for example, if your package is com.exercise.AndroidClient and your service class is under com.exercise.AndroidClient.services.Communication you need to declare the service like this:
<service android:enabled="true" android:name=".services.Communication" />
Or specify the full package:
<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" />

I had the same error and the cause was, that the service was not defined in the AndroidManifest.xml.

Also, if your service happens to exist in a library project that you are refering to, check your project.properties file.
You have to add this line:
manifestmerger.enabled=true

Related

Unable to start service Intent from another app

I know this question has been a lot of times already, but I tried, I think, all the accepted answers, but it didn't solve my problem. I might be missing something, being new to Services and these kind of intents.
I have two apps, and one of them needs to call a service implemented in the second one.
In my app being called, I declared this in manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp">
<application
android:name=".MyApplication">
<service
android:name="com.myapp.SynchronizationService"
android:exported="true"
android:enabled="true" >
</service>
</application>
</manifest>
My service being declared as follows :
package com.myapp;
/**
* Sync service
*/
public class SynchronizationService extends Service {
#Override
public void onCreate() {
super.onCreate();
initSync();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void initSync() {
//do some work
}
}
And then, in my app calling the service, I have declared this :
val i = Intent().apply {
component = ComponentName("com.myapp", "com.myapp.SynchronizationService")
}
val c: ComponentName? = startService(i)
And in AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.intentcaller">
<application>
...
</application>
<queries>
<package android:name="com.myapp" />
</queries>
</manifest>
But when I try to call this, I get this error in logs :
Unable to start service Intent { cmp=com.myapp/.SynchronizationService } U=0: not found
Thanks
You can declare permission in the app with service and use this permission in the app when you start intent

Switch background service to foreground when the app quits [duplicate]

I'm working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when user clicks back back until app minimizes, in that case first activity is also getting destroyed). Please help.
I want stop the service when user removes the application from the
recent list.
Yes, you can either do that using stopWithTask flag as true for Service in Manifest file.
Example:
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="true" />
OR
If you need event of application being removed from recent list, and do something before stopping service, you can use this:
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="false" />
So your service's method onTaskRemoved will be called. (Remember, it won't be called if you set stopWithTask to true).
public class MyService extends Service {
#Override
public void onStartService() {
//your code
}
#Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want
//stop service
this.stopSelf();
}
}
Hope this helps.
The last answer didn't work for me (I am using API 29).
I looked up in the Javadoc of the onTaskRemoved function and saw the following instruction:
"If you have set {android:stopWithTask}, then you will not receive this callback; instead, the service will simply be stopped."
So, all I got to do is to remove this line (android:stopWithTask="false") from the manifest and it worked just fine.
This is the updated code in the manifest file:
<service
android:enabled="true"
android:name=".ExitService"
android:exported="false"/>
Im posting this answer, since the chosen as best solution was not working for me.
This is the updated version:
First create this service class:
public class ExitService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want before app closes.
//stop service
this.stopSelf();
}
}
Then, declare this way your service in the manifest label:
<service
android:enabled="true"
android:name=".ExitService"
android:exported="false"
android:stopWithTask="false" />
Now, just start the service wherever you want to do something before your app closing.
Intent intent = new Intent(this, ExitService.class);
startService(intent);

NotificationListenerService not working - even after giving permission

Below is my code to capture notifications. I dont understand why the onNotificationPosted is not getting fired.
I am giving my app Notifications access from Settings > Security and the onCreate from MyNotifService is also getting fired, but onNotificationPosted is not getting fired.
What am I missing or doing wrong?
From my MainActivity's onCreate() function:
Intent intent = new Intent(this, MyNotifService.class);
this.startService(intent);
My Service code that extends NotificationListenerService:
#SuppressLint("NewApi")
public class MyNotifService extends NotificationListenerService {
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.v("focus", "in onNotificationPosted() of MyNotifService");
}
#Override
public void onCreate() {
super.onCreate();
Log.v("focus", "in onCreate() of MyNotifService");
}
}
In the manifest file, I have this:
<service android:name="com.mavdev.focusoutfacebook.notifications.MyNotifService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
It does seem to be a bit flakey, but this is the actual fix in this case:
#Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
I've been trying to get it working for the second day, and I succeeded. Try adding these lines at the end of onStartCommand method.
And, it hardly plays a role, but try to declare your service after activities in the manifest file.
This comment with sample code was really helpful to me: https://stackoverflow.com/a/41521664/10652152

How to detect the application has closed from Recent Apps [duplicate]

I'm working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when user clicks back back until app minimizes, in that case first activity is also getting destroyed). Please help.
I want stop the service when user removes the application from the
recent list.
Yes, you can either do that using stopWithTask flag as true for Service in Manifest file.
Example:
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="true" />
OR
If you need event of application being removed from recent list, and do something before stopping service, you can use this:
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="false" />
So your service's method onTaskRemoved will be called. (Remember, it won't be called if you set stopWithTask to true).
public class MyService extends Service {
#Override
public void onStartService() {
//your code
}
#Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want
//stop service
this.stopSelf();
}
}
Hope this helps.
The last answer didn't work for me (I am using API 29).
I looked up in the Javadoc of the onTaskRemoved function and saw the following instruction:
"If you have set {android:stopWithTask}, then you will not receive this callback; instead, the service will simply be stopped."
So, all I got to do is to remove this line (android:stopWithTask="false") from the manifest and it worked just fine.
This is the updated code in the manifest file:
<service
android:enabled="true"
android:name=".ExitService"
android:exported="false"/>
Im posting this answer, since the chosen as best solution was not working for me.
This is the updated version:
First create this service class:
public class ExitService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
System.out.println("onTaskRemoved called");
super.onTaskRemoved(rootIntent);
//do something you want before app closes.
//stop service
this.stopSelf();
}
}
Then, declare this way your service in the manifest label:
<service
android:enabled="true"
android:name=".ExitService"
android:exported="false"
android:stopWithTask="false" />
Now, just start the service wherever you want to do something before your app closing.
Intent intent = new Intent(this, ExitService.class);
startService(intent);

Android app crashes on Device Boot

I am working on an Application that requires to start a BackgroundService on Android Boot
This is the code that I am using to start the BroadcastReceiver on Android Boot
public class StartOnBootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try
{
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.package.myApplicationPackage.BackgroundService");
context.startService(serviceIntent);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This is my BackgroundService.class
public class BackgroundService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//code to execute when the service is first created
Toast.makeText(getBaseContext(), "BACKGROUND SERVICE STARTED", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
//code to execute when the service is shutting down
}
#Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}
This is the error log that I managed to snipe from the CatLog while my Android was Booting.
12-21 10:28:01.279: E/EmbeddedLogger(1710): App crashed! Process: com.package.myApplicationPackage
12-21 10:28:01.289: E/EmbeddedLogger(1710): App crashed! Package: com.package.myApplicationPackage v1 (1.0)
12-21 10:28:01.289: E/EmbeddedLogger(1710): Application Label: myApp Label
My AndroidManifest.xml file
</service>
<service android:name=".BackgroundService">
<intent-filter>
<action android:name="com.package.myApplicationPackage.BackgroundService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartOnBootService"
android:enabled="true"
android:exported="true"
android:label="StartOnBootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You Broadcast receiver name and android manifest receiver name is totally differ.
StartOnBootService or in manifest it is StartMyServiceAtBootReceiver
Maybe it's a good practice to look at the manifest file before rushing over stackoverflow.
I also had a similar problem, "crash after boot" and noticed that I had a wrong package name, as a result of a hasty copy paste action for one of my receivers.
<receiver android:name="com.somepackage.broadcast.ConnectionChangeReceiver" />
was indeed having a typo as
<receiver android:name="com.somepackage.broadcast.broadcast.ConnectionChangeReceiver" />
Correcting it obviously solved my problem.

Categories

Resources