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.
Related
I have a custom button in a sticky notification.
I used to attach a PendingIntent to it for receiving button clicks:
Intent intent = new Intent();
intent.setAction("com.example.app.intent.action.BUTTON_CLICK");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2000, intent, PendingIntent.FLAG_UPDATE_CURRENT);
contentViewExpanded.setOnClickPendingIntent(R.id.button, pendingIntent);
When i run this code on Oreo , i get BroadcastQueue: Background execution not allowed in logcat and don't receive button click.
I registered receiver with manifest:
<receiver
android:name=".BroadcastReceiver.NotificationActionReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.example.app.intent.action.BUTTON_CLICK"/>
</intent-filter>
</receiver>
I also tried registering receiver in my code:
NotificationActionReceiver mMyBroadcastReceiver = new NotificationActionReceiver();
IntentFilter filter = new IntentFilter("com.example.app.intent.action.BUTTON_CLICK");
mContext.registerReceiver(mMyBroadcastReceiver, filter);
This works but only when the app is visible to user.
Thanks for help
Never use an implicit Intent when an explicit Intent will work.
Replace:
Intent intent = new Intent();
intent.setAction("com.example.app.intent.action.BUTTON_CLICK");
with:
Intent intent = new Intent(this, NotificationActionReceiver.class);
And remove the <intent-filter> from the NotificationActionReceiver <receiver> element.
I ran into this issue as well on Android 8 - Oreo, but given my library project requirements, I don't have the explicitly named BroadcastReceiver class implementation, that the end-client will declare in it's AndroidManifest.
Solution:
Specify the application package on the Intent using setPackage(String).
Example:
// Application unique intent action String
final String receiverAction = getApplicationContext().getPackageName()
+ BaseLibraryReceiver.ACTION_SUFFIX;
// No need for Class definition in the constructor.
Intent intent = new Intent();
// Set the unique action.
intent.setAction(receiverAction);
// Set the application package name on the Intent, so only the application
// will have this Intent broadcasted, thus making it “explicit" and secure.
intent.setPackage(getApplicationContext().getPackageName());
...
From the Android Broadcasts: Security considerations and best practices docs.
In Android 4.0 and higher, you can specify a package with
setPackage(String) when sending a broadcast. The system restricts the
broadcast to the set of apps that match the package.
Here’s an example of the BroadcastReceiver declared (or merged) in to the end-client application’s AndroidManifest:
<receiver
android:name=“com.subclassed.receiver.ReceiverExtendedFromLibrary"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="${applicationId}.action.MY_UNIQUE_ACTION"/>
</intent-filter>
</receiver>
Since my example revolves around a library project that broadcasts an Intent, I’ve decided to keep the <intent-filter> and <action /> in the <receiver> declaration. Otherwise, there would be non-unique broadcast actions being fired, which could lead to potential issues where multiple applications receive the wrong broadcast. This is mostly a safety precaution. Of course you still need to check the action in the implementation of the BroadcastReceiver.
Hope someone finds this helpful!
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 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.
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
When my android app is removed, I would like to also remove files the app has created on the SD card, as these can consume many megabytes and are only of use to my app.
It seems that receiving the PACKAGE REMOVED intent would be the place to do this.
However, my broadcast receiver is never called--it seems to have been deleted before the PACKAGE REMOVED intent is sent
The code is:
public class UninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action= intent.getAction();
Log.i("U", "ACTION " + action);
etc.
}
}
and, in the manifest:
<application android:debuggable="true"
android:icon="#drawable/icon"
android:label="#string/app_name">
<receiver android:name ="com.boom.UninstallReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
The documentation says:
The package that is being removed does not receive this Intent.
Android 2.2 added getExternalFilesDir(), which will point to a place on the external storage that Android will automatically clean up when your application is uninstalled. However, that is only for Android 2.2, and there are indications that it does not work particularly well at the moment. However, it is something to keep in mind for 2011.
Beyond that, all you can really do is offer a menu choice somewhere for the user to do the cleanup, and hope users use it before uninstalling you.
Have you tried this?
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addDataScheme("package");
registerReceiver(new UnInstalledReceiver(), filter);
Also, putting the files of your app in the folder android/data/com.boom/ is a nice easy way to make sure extra files get cleaned up when users uninstall.
You must add the permission in the manifast.
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />