I have a weird issue with my Service not starting. I have my manifest file with the service, and have called it. But still it does not open up.
<service
android:name=".com.taxeeta.ForHire"
android:enabled="true" />
Calling the intent
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.taxeeta.ForHire");
startService(serviceIntent);
Service
public class ForHire extends Service
I wonder what I am missing here.
Change
android:name=".com.taxeeta.ForHire"
with
android:name="com.taxeeta.ForHire"
or if the service is on the root package
android:name=".ForHire"
Also, you should use Intent.setClass( ) instead of setAction, since you don't have an IntentFilter declared for your service and you most likely, trying to use an explicit intent.
Just call startService(new Intent(getApplicationContext(),ForHire.class));
Every thing is fine in your menifest.
No need to set Action according to your menifest.
When you Declare service in Manifest file use like this.
<service android:name=".ForHire">
<intent-filter>
<action android:name="com.taxeeta.ForHire" />
</intent-filter>
</service>
& call service Like this way.
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.taxeeta.ForHire");
startService(serviceIntent);
For More information about Service refer this Documentation
http://developer.android.com/guide/components/services.html
You have a problem in the declaration of the service in your manifest. Change it to:
<service android:name="com.taxeeta.ForHire" />
(notice the . [dot] removed). Also make sure service is a child element of your application element, which is a must for the service to be recognized by the Android OS.
Related
I have a service that supposed to upload photos in the background. But when I try to start it, it doesn't start. In the logcat I've noticed that I get a warning Implicit intents with startService are not safe : Intent { .....}. I've already tripled checked that the action string is the same in the manifest and what I'm starting with. My code :manifest :
<service android:name=".photosupload.services.PhtosUploadService"
android:exported="false" android:process=":uploadPhotosServiceProcess">
<intent-filter>
<action android:name="com.yoovi.app.photosupload.services.action.START_UPLOAD" />
</intent-filter>
</service>
starting service code :
Intent i = new Intent(PhtosUploadService.ACTION_START_UPLOAD);
i.putExtra(PhtosUploadService.ALBUM_ID, albumID);
context.startService(i);
You need to understand implicit and explicit intents.
Explicit intent means you need to specify the exact class from which the intent will be serviced.
Your code should be similar to
Intent i = new Intent();
i.setClass(this, photosupload.services.PhtosUploadService.class);
If you want to send implicit intent to a service - Please Change to android:exported="true" in manifest.
Something is still not clear for me; I have to monitoring the battery level and i wrote inside my service in th onCreate this lines:
public void onCreate(){
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
}
Then in the Manifest:
<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
What i don't understand is.. Is it correct write the Intent Intent.ACTION_BATTERY_CHANGED in the java and also android.intent.action.BATTERY_CHANGED in the Manifest? Or just need only one?
Whenever you are planning on using an Intent Filter, always check with the Intent documentation. If the broadcast cannot be picked up by the manifest Intent Filter, it will usually be described in the documentation. For example, the BATTERY_CHANGED broadcast action provides this information:
You can not receive this through components declared in manifests,
only by explicitly registering for it with Context.registerReceiver().
You only need 1. Writing it in the manifest makes it active as soon as the app is installed. Putting it in the java code will make it active only when the activity/service its created in is running.
Both ways are correct. You can write either in Manifest file or in java file. Some permissions must be write in Manifest file.
from android developers : "Components(service) advertise their capabilities — the kinds of intents they can respond to — through intent filters.
I just cant understand the purpose of intent filter inside service in the Manifest.xml, what is the capability here?
<service
android:name="com.x.y"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.x.y" />
</intent-filter>
</service>
and what's he difference if i remove the intent-filter?
<service
android:name="com.x.y"
</service>
thanks.
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.
You can use intent filters for explicitly calling your service or get your service to be implicitly called where components from any application installed on the user's device can potentially start your service
If you plan on using your service only locally (other applications do not use it), then you don't need to (and should not) supply any intent filters
It is clearly specified in the documentation Declaring a service in the manifest
I have a broadcast receiver registered in Manifest:
<application ...>
<receiver android:name="com.some.pkg.NewAppReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
</intent-filter>
</receiver>
</appcication>
And the receiver:
public class NewAppReceiver extends BroadcastReceiver {
private static final String TAG = "NewAppReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Intent: " + intent.getAction());
}
}
And nothing is received when I install APK manually or from the Android Market. Why?
Did you run the app that contains this broadcastReceiver before installing the other apps?
Starting at some API version, broadcastReceivers will not work till you execute the app. Put an activity and execute it.
Also , don't forget to add the following into the broadcastReceiver:
<data android:scheme="package" />
EDIT: On Android 8 and above, if your app targets API 27 or more, it will work partially, so you have to register to those events in code and not in manifest. Here's a list of intents that are still safe to use in manifest: https://developer.android.com/guide/components/broadcast-exceptions.html .
The rest should be used in code. More info here
Since android.intent.action.PACKAGE_ADDED is a System Intent (note that your own app will not receive it at its installation), your BroadcastReceiver will receive messages from sources outside your app. Thus, check you did NOT put: android:exported="false"
You also may need to add:
<data android:scheme="package" />
So, your BroadcastReceiver in your AndroidManifest.xml should look like this:
<application ...>
<receiver android:name=".NewAppReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</appcication>
If it still doesn't work, you may try to put an higher priority, such as: android:priority="1000"
Take a look at: http://developer.android.com/guide/topics/manifest/receiver-element.html
Registering receiver from manifest would not work from API 26(android 8). Because it had performance impact on older versions.
But we can register receiver from java code and receive updates of removed and added applications.
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
intentFilter.addDataScheme("package")
registerReceiver(YourBroadcastReceiver(), intentFilter)
Are you trying to receive the intent in the application you are installing? The documentation for ACTION_PACKAGE_ADDED says:
Note that the newly installed package does not receive this broadcast.
Another possibility is that this intent might not be delivered to components registered via the manifest but only manually (as described in an answer by Mark Murphy to Stack Overflow question Can't receive broadcasts for PACKAGE intents).
If you try to receive some other package it must be worked.
(As #Savvas noted) If you try to receive your own package's addition you can't receive it. Even if your broadcast receiver has action.PACKAGE_ADDED, receiver's onReceive method isn't triggered.
In this case your best bet is saving this data. By using sharedPreferences, add a key something like "appIsWorkedBefore", and on your launcher Activity's onCreate method set this variable as "true". And you can make your works with respect to this Boolean.
This intent action is no longer available for applications.
This is a protected intent that can only be sent by the system.
https://developer.android.com/reference/android/content/Intent#ACTION_PACKAGE_ADDED
I have created a activity that is only meant to be launched from a link (using a intent filter.) I do not want this activity to have a GUI - I just want it to start a service and put a notification in the bar. I have tried to put the intent filter for the link in my service, but that does not work. Is there a better thing to do this that will answer to intent filters - or can I just make my activity not have a GUI?
Sorry if I'm being confusing, Isaac
Echoing previous response, you shouldn't use a broadcast receiver.
In the same situation, what I did was to declare the theme thusly:
<activity android:name="MyActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoDisplay">
Your best bet would seem to be using a BroadcastReceiver. You can create a new BroadcastReceiver that listens for the Intent to trigger your notification and start your service like this:
public class MyIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context _context, Intent _intent) {
if (_intent.getAction().equals(MY_INTENT)) {
// TODO Broadcast a notification
_context.startService(new Intent(_context, MyService.class));
}
}
}
And you can register this IntentReceiver directly in the application Manifest without needing to include it within an Activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.myapplication">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:enabled="true" android:name="MyService"></service>
<receiver android:enabled="true" android:name="MyIntentReceiver">
<intent-filter>
<action android:name="MY_INTENT" />
</intent-filter>
</receiver>
</application>
</manifest>
I'm not sure if a service would work, but a broadcast receiver definitely would not. Url's are launched using startActivity(). Broadcast receivers cannot respond to this.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
FTA:
Note that, although the Intent class is used for sending and receiving these broadcasts, the Intent broadcast mechanism here is completely separate from Intents that are used to start Activities with Context.startActivity(). There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); likewise, when you broadcast an Intent, you will never find or start an Activity.
Use Service. I works definitely. When you click the program, it would do its work without any GUI. Use pendintgintent...getService(MySerice.class....). Then, create a new class MyService extending the Service class. Inside MyService.class, override onStart() and do whatever you want to do.