Custom Intent Broadcast not working - android

I'm trying to send a custom intent to my service that is based off IntentService but when I do context.sendBroadcast nothing seems to happen. I've checked through the logcat logs and can't even see the intent resolution failing.
My service registration in my Android.xml is
<service android:name=".Service.FbSlideShowService" android:enabled="true" >
<intent-filter>
<action android:name="com.test.fbslide.UPDATE_WALLPAPER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
I'm trying to send the broadcast from a helper class on my activity thread and passing in context using, I've tried sending it in 2 ways:
Intent changeWallpaperIntent = new Intent(mContext, FbSlideShowService.class);
mContext.sendBroadcast(changeWallpaperIntent);
And
Intent changeWallpaperIntent = new Intent(FbSlideShowService.UPDATE_WALLPAPER_INTENT, null);
mContext.sendBroadcast(changeWallpaperIntent);
But the broadcast just doesn't work.
Any ideas?

sendBroadcast works when you have a BroadcastReciever. But what you have here is a sevice
<service android:name=".Service.FbSlideShowService" android:enabled="true" >
<intent-filter>
<action android:name="com.test.fbslide.UPDATE_WALLPAPER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
Your manifest entry tells that it is a service
You need to use mContext.startService() to start the service.
If you want to start a service when device boots you can refer this answer.

Related

is having two broadcast receivers valid in android?

i don't know if this is okay having two broadcast receivers in my android app. I separated them cause the one receiver with the boot_completed, it will do a different task and the other receiver with also do different task when they receive a broadcast.
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".AlarmManagerBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
That is perfectly valid you should always try to seperate your concerns which part of a good architecture/good design principles.

Cannot receive Uninstall broadcast

I am trying to detect the uninstall action of my application. Till now, I have got a specific code that catch the uninstall action and inflate an Activity. Here is the code:
Manifest:
<activity
android:name=".UninstallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
I have created a simple Activity called UninstallActivity and It works fine. When the user try to uninstall the app this Activity has been inflated.
I am trying to listen on those intents with a Receiver instead of Activity but I have failed to get this action. My code is:
Manifest:
<receiver android:name=".PackageUninstallReceiver" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
PackageUninstallReceiver:
public class PackageUninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("uTag", "In the PackageUninstallReceiver onReceive()");
if (intent.getAction().equals(Intent.ACTION_DELETE) && intent.getDataString().contains(context.getPackageName())) {
Log.d("uTag", "Uninstallation is being happened....");
}
}
}
First, is it possible to listen to this Intent with the receiver?
If yes, what is wrong with my code?
The Intent used to start an Activity (in this case, an Intent to VIEW or DELETE a PACKAGE) is a completely different thing from a braodcast Intent. They share some of the same properties, but are still completely different things. a broadcast Intent will never start an Activity and an Intent used to start an Activity will never be seen by a BroadcastReceiver.
Therefore, the answer to your question
First, is it possible to listen to this Intent with the receiver?
is "no".
The actions you are listening are generic and could be applied in any context with a different schema. What you should be listening to is the package changing.
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>

Make a link in the Android browser call an intent receiver

I would like to make a link in Android browser to be able to call a receiver.
I read this post on how to make a link start my app and tries to do the same for my startup receiver receiver, but it didn't work.
Is it possible?
My receiver is configured as follows:
<receiver
android:name="com.wiziapp.wiziappweb.app.MyStartupIntentReceiver"
android:label="#string/search" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
<intent-filter>
<data android:scheme="myapp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</receiver>
The link I use to test it is:
Enable notification
I would like to make a link in Android browser to be able to call a receiver.
That is not possible.
My receiver is configured as follows:
ACTION_VIEW is used with startActivity(), not sendBroadcast(), so your second <intent-filter> is unlikely to ever be used.
The link I use to test it is
Links only call startActivity(), not sendBroadcast(). Your are welcome to have the link point to a Theme.NoDisplay activity that sends the broadcast and then calls finish() to go away.

Can I start a service without activity or receiver?

I want to start a service in an APK.
I tried to use as following:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name =".TestServcie">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>
Any ideas?
Thanks
You can write a BroadcastReceiver and run the Service after receiving the Intent. For example after device boot-up or other Intent that you need.
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
No you can't.
Create a simple Activity which starts the service and simply provides some feedback to the user (to tell them the service has started for example) and set that Activity with the MAIN/LAUNCHER intent.

Can't receive broadcasts for PACKAGE intents

I am trying to register a Broadcast Receiver to receive broadcast events for the package events. Following is the code and my receiver in the manifest file. The log statment never happens, but I can clearly see the same broadcast firing for "HomeLoaders" (the Launcher) debug statements. What am I missing?
public class IntentListener extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("INTENT LISTNER:", intent.getAction());
}
}
<receiver android:name="IntentListener" android:enabled="true" android:exported="true">
<intent-filter>
<data android:scheme="package"></data>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.PACKAGE_CHANGED"></action>
</intent-filter>
</receiver>
It is possible that these Intents cannot be received by components registered in the manifest, but only by receivers registered in Java via registerReceiver().
These three intents namely,
Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED
when broadcasted by the system, have
Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
flag added so that only the registered receivers will receive the broadcasts and no broadcast receiver components will be launched. Refer Intent and PackageManagerService class of source for further details.
This is my manifest, without
<category android:name="android.intent.category.DEFAULT" />
My app detects only the Android Market app install, but does not remove. Now it receives also the non-Android Market app broadcasts.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SomeActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.som.pakage.PackageInstallReceiver" >
<intent-filter >
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>

Categories

Resources