Broadcast receiver not receiving intent - android

I have two apps that I have complete control over. Both are signed with the same cert and both use the exact same intent filter. One sends the broadcast from a fragment, the other is suppose to receive it and do something. This however is not working:
Strings.FILTER_INIT_REGISTER = "com.app.FILTER_INIT_REGISTER"
Intent intent = new Intent(Strings.FILTER_INIT_REGISTER);
getActivity().sendBroadcast(intent);
I have registered the receiver in the Manifest app tag for the app containing the ReportingReceiver class:
<receiver
android:name=".receivers.ReportingReceiver"
android:exported="true"
>
<intent-filter>
<action android:name="com.app.FILTER_INIT_REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Curious why the ReportingReceiver class is not getting the intent call?

If your application only has a service and receivers then this won't work in Android 3.1 and later. The reason is that the system will not send broadcast Intents to application that are in the STOPPED STATE. An application is in the STOPPED STATE when it is first installed. It is removed from the STOPPED STATE when the user manually starts the application for the first time. It is returned to the STOPPED STATE if the user forces the application to stop using the application manager tool.
Since your application has no Activities, there is no way for the user to "start" it. Therefore it will never come out of the stopped state.
See http://developer.android.com/about/versions/android-3.1.html#launchcontrols

As Android Addict says in his comment to David Wasser's answer ... there is a way around this behaviour.
Just add the following flag to the calling Intent. This will ensure that you also reach broadcast receivers from "stopped" applications.
http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES
You can read more about this Android 3.1 change here
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
and here
http://code.google.com/p/android/issues/detail?id=18225

Related

POC - Start Service on boot without an Activity - Android 4+

This question may seem a trivial but I've been struggling with it.
I'm trying to start a service on boot and everything works fine if I start it at least once from the mainActivity (launch activity), something like:
AndroidManifest.xml
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<activity
android:name="com.example.mainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.bootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="com.example.someService" android:enabled="true" android:exported="false"/>
...
bootReceiver.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
mainActivity.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
With the above code I'm able to run the service after every boot without any problems.
As a POC, I'm trying to start a service without any activity, or at least without the mainActivity, just by declaring the service on the AndroidManifest.xml or by creating new activity (invisible?) that is launched at the same time as the default launcher activity.
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Is there any way to achieve this ?
Can I start 2 activities from the AndroiManifest.xml when the user launches the app?
I'm sorry if my question isn't 100% clear, I've tried to explain it the best I could, if you cannot understand it, please leave a comment bellow. Tks.
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Android 3.1, actually, to help prevent drive-by malware.
Is there any way to achieve this ?
Something has to use an explicit Intent to invoke some form of IPC on one of your components, to move the app out of the so-called "stopped state" that is preventing you from receiving the broadcast. So, something needs to either:
start one of your activities via an explicit Intent, or
start one of your services via an explicit Intent, or
send a broadcast to one of your receivers via an explicit Intent
(I don't know if trying to connect to a ContentProvider will work, though arguably it should)
The key is the explicit Intent. That's why invoking an activity from a home screen launcher works, because the Intent used to start your activity will be an explicit one.
However, in the absence of such an activity, you would need to find something else that would use an explicit Intent to invoke one of your components. Certain specialized services (e.g., an input method) probably get invoked with an explicit Intent if and when the user activates that app's capability via the system Settings app. If you're a plugin for some other app, that other app might use an explicit Intent to work with one of your components. You can ask the user to install the Android SDK, learn how to use the command line, and invoke an adb shell am command to start one of your components. And that's about all I can think of off the top of my head. None are exactly general purpose solutions.
or by creating new activity (invisible?) that is launched at the same time as the default launcher activity
I have no idea what you think that would achieve. If the user starts up your launcher activity, you're already out of the stopped state and will receive broadcasts as normal.

Can I start NotificationListenerService in code explicitly rather than it be started for me by the OS?

I have class derived from NotificationListenerService and it is getting created and started for me automatically when the app starts.
However I would like to lazily start the service later from an activity and not have it start automatically when the app starts. Is it possible to do this?
The manifest is:
<service android:name=".MyNotificationListener"
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>
NOTE that before NotificationListenerService can be used the user has to grant the app notification access via the setting. However if the intent filter is removed from the manifest then the app does not appear in the settings and thus the user is unable to grant permission, so without permission the service can't be started. Seems like the OS needs that intent filter to be there before it will display the app in the settings.
UPDATE:
The documentation for BIND_NOTIFICATION_LISTENER_SERVICE says:
Must be required by an NotificationListenerService, to ensure that
only the system can bind to it.
So I guess that means only the OS can start NotificationListenerService and nobody else can.
Yes. Create an Intent for the service then call startService.

What determines app auto start on boot in Android?

In my application, there's a feature that allows users to dial a specific number and brings up an activity to front. I have the following receiver, and the only receiver registered in AndroidManifest.xml.
<receiver android:name="com.example.myapp.OutgoingCallListener" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Please note there's no BOOT_COMPLETED intent or service.
Now here's the thing I couldn't figure out. When I reboot my device, go check the Running Apps, my application is not listed there. But, if I dial the specific number, my application starts and the activity is brought to front.
My question is: If the app is not a service, and not started on boot, how could it recieve intent from Android? That is, in my case, how could my app listen to NEW_OUTGOING_CALL while it's not started at all?
A BroadcastReceiver that is registered in the manifest is always capable of responding to a matching broadcast. If your process is not running for any reason, Android will start up a process for you.

Restarting an app via an external Broadcast Receiver

I have implemented an app that is basically a custom app store for updating and launching a family of related apps. It also needs to update itself, which works, but the app is killed without warning during the install process. I want to automatically restart the app in this case so that the user can continue to use it immediately after an update.
So I made a separate application including only a single Broadcast Receiver that listens for package events for the first app's package name and starts a new activity. That receiver is never called:
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<receiver android:name=".AppUpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
In searching for similar implementations I have seen directly contradictory information on whether an application with only a receiver will ever execute, and whether a receiver will be called if its app is not already running. I've even come across example code of an application containing only a receiver, with a manifest very similar to my own. So what do I need in this application to ensure that the receiver is called whenever another package is installed?
If there is a better solution, I'd be happy to hear it.
Depending on the version of Android, you might need to start an application component in order for the BroadcastReceiver to be registered. By this I mean there will need to be a launcher Activity which must be started manually by the user.
From Honeycomb (I think) onwards it isn't possible to have application components 'active' unless the app has been manually started in some way. The reasoning behind this is the potential for insecure code executing without the end-users' knowledge.
I suspect this is what you're experiencing. To test it, add a simple "Hello World" Activity to the app that has the BroadcastReceiver in it. Launch the Activity and then check to see if the BroadcastReceiver then gets called after your other package is updated.

Android - [Xoom/Honeycomb] application without LAUNCHER activity does not work

I have an application without launcher activity that works properly from Android 1.5 to Android 2.3.4. It is started by my broadcast receiver. However, on Honeycomb (Motorola Xoom), my broadcast receiver doesn't work at all (it does not catch any intents). If I add launcher activity to my manifest:
<activity android:label="#string/app_name" android:name="com.myapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity>
Then my app works properly (broadcast receiver catches all necessary intents and starts my services).
I would be very grateful for help!
Are you running Honeycomb 3.1 or above? If yes take a look here.
When your application is installed, it is in stopped state. When the application is first launched, it is moved out of stopped state.
A application in stopped state won't get started by all broadcast intents. The sender of the broadcast intent has to specify the Intent.FLAG_INCLUDE_STOPPED_PACKAGES flag if it wants to launch stopped applications too.
Intent intent = new Intent(MY_INTENT_ACTION);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
If you can't change the code which sends the intent, your best bet would be to keep the launcher activity. Whenever the user launches your application after installation, it will be moved out of the stopped state and you will start receiving broadcasts.
Note the the user can move your application back to the stopped state from Manage Applications in device settings.
On Android 3.1 and higher, BroadcastReceivers will not be invoked until the user has started the application at least once by some other means, such as launching an activity.
Since users tend to get very confused by applications that do not have an activity, you really should have had an activity in the first place, for online help if nothing else. With Android 3.1, that is even more important.

Categories

Resources