How Is an Intent Service Declared in the Android Manifest

How is an Intent Service Declared in the Android Manifest?

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" />

Purpose of Service Intent-Filter inside Manifest.xml

If you want to use a service to perform different actions, then declaring an intent filter will help your service match against different actions you want to perform.

The example will explain better.

Suppose you have following declaration in manifest file:

<service
android:name="MyService" >
<intent-filter>
<action android:name="com.x.y.DOWNLOAD_DATA" />
<action android:name="com.x.y.UPLOAD_DATA" />
</intent-filter>
</service>

Then in your IntentService you could filter for these actions like this:

public class MyService extends IntentService {

public MyService() {
super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
if(intent.getAction().equals("com.x.y.DOWNLOAD_DATA"){
//download data here
}else if(intent.getAction().equals("com.x.y.UPLOAD_DATA"){
// upload data here
}
}
}

Basically, it allows you to use the same service for different actions, instead of creating two separate services for example.

However, having intent filters declared for a service is not regarded as a good practice, and this is what the docs had to say:

Caution: To ensure your app is secure, always use an explicit intent
when starting a Service and do not declare intent filters for your
services. Using an implicit intent to start a service is a security
hazard because you cannot be certain what service will respond to the
intent, and the user cannot see which service starts.

What is the use of intent-filter in the manifest for a service?

As mentioned in the other answers, the intent filter helps your service classify the type of action to execute. So for example, you can try starting the service like below:

Intent intent = new Intent(this, MyService.class);
intent.setAction("INTENT_SAMPLE");
startService(intent);

Then in MyService class, check for the action like so:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if("INTENT_SAMPLE".equals(intent.getAction())) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}

Since https://developer.android.com/guide/components/intents-filters#Types does not recommend intent filters for services, drop the action from manifest. To achieve the same result as above, simply add an extra to your intent like so:

Intent intent = new Intent(this, MyService.class);
intent.putExtra("INTENT_SAMPLE", true);
startService(intent);

Then in MyService class, check for the action like so:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.getBooleanExtra("INTENT_SAMPLE", false)) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}

Cheers!

How to start an IntentService from a WakefulBroadcastReceiver

There is no tag as <intentservice> in Application Manifest. IntentService is a subclass of Service, so you need to declare it as service in manifest.


Change

<intentservice 
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</intentservice>

to

<service 
android:name="com.example.broadcasttest.MonitorService"
android:enabled="true" >
<intent-filter>
<action android:name="com.example.broadcasttest.MonitorService" />
</intent-filter>
</service>

Read service name from AndroidManifest

Use below utility method

public static void startService(Context context, String lookupAction) {

Intent serviceIntent = new Intent();
serviceIntent.setAction(lookupAction);
serviceIntent.setPackage("package.of.your.application");

List<ResolveInfo> resInfo = context.getPackageManager().queryIntentServices(serviceIntent, 0);
if (resInfo != null && !resInfo.isEmpty()) {

ServiceInfo service = resInfo.get(0).serviceInfo;
ComponentName cmpService = new ComponentName(service.applicationInfo.packageName, service.name);

Intent serviceToStart = new Intent(lookupAction);

serviceToStart.setComponent(cmpService);

context.startService(serviceToStart);
} else {
// Handle error
}
}

I will add documentation soon

IntentService is not being called

Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>

</application>
</manifest>

Can I put an Intent Service into a library project and use it in different apps?

there will not be any conflict with that scenes each process running independently it own instances, and the fact that both of them running same service name (and package name) don't cause any problem.

I know that for sure, simply because I'm doing the same thing myself for a long time - using same library Service in two different apps that runs simultaneity on same device

under the hood, Service is simply implemented as kind of singltone that the OS manage it lifecyle in special way then other Java classes, but eventually it's still an instance running inside your own process memory sandbox.

Same with IntentService, that extends Service



Related Topics



Leave a reply



Submit