Note that there are some explanatory texts on larger screens.

plurals
  1. POBroadcastReceiver with IntentFilter for Intent.ACTION_PACKAGE_REPLACED not working within a service
    primarykey
    data
    text
    <p>I am trying to set up a service that checks when a new update of an activity is installed in a device. I have already done so within an application activity, declaring the <code>Broadcastreceiver</code> in the manifest and it works perfectly. </p> <p>However, when I try to run that receiver within a Service and dynamically declare it, my <code>onReceive</code> never gets called. This is my Service code:</p> <pre><code>public class UpdateService extends Service { private static String mPackage = "com.my.package"; private static String mActivityName = "myActivity"; private BroadcastReceiver mUpdateReceiver; @Override public void onCreate() { super.onCreate(); mUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("mTag","In the BroadcastReceiver onReceive()"); if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_REPLACED)) { // Log that a new update is has been found Log.d("mTag","New version of the app has been installed."); Log.d("mTag", "Intent data: " + intent.getDataString()); Log.d("mTag","My package: " + mPackage); } } }; Log.d("mTag","In the service onCreate() method."); IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_PACKAGE_REPLACED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_ADDED); registerReceiver(mUpdateReceiver,filter); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("mTag","UpdateService started"); return Service.START_STICKY; } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(mUpdateReceiver); Log.d("mTag","Service destroyed"); } @Override public IBinder onBind(Intent arg0) { return null; } } </code></pre> <p>The Log in the Service onCreate() gets called, which tells me that the service is up and running. However, after installing and replacing some apps through the adb, none of the logs in the <code>BroadcastReceiver</code> the method <code>onReceive()</code> get called.</p> <p>This is my MainActivity:</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, UpdateService.class)); } } </code></pre> <p>Do you guys have any ideas why the <code>onReceive()</code> does not get called?</p> <p>Thank you.</p> <p>I based my code in these two references:</p> <ul> <li><a href="https://stackoverflow.com/questions/9092134/broadcast-receiver-within-a-service">BroadcastReceiver within a Service</a></li> <li><a href="https://stackoverflow.com/questions/2133986/how-to-know-my-android-application-has-been-upgraded-in-order-to-reset-an-alarm">How to know Android app upgraded?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload