Broadcastreceiver and Paused activity

后端 未结 3 1932
遥遥无期
遥遥无期 2020-12-08 08:06

I have a broadcast receiver registered programatically in a activity. It responds to the PACKAGE_REMOVED intent, that fires when a package gets removed.

<
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 08:59

    Add a Receiver to your project and you will get this event without even starting your application.

    public class TestReciver extends BroadcastReceiver  {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("TestReciver",intent.getAction()+"\n"
                    +intent.getDataString()+"\n"
                    +"UID: "+intent.getIntExtra(Intent.EXTRA_UID,0)+"\n"
                    +"DATA_REMOVED: "+intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false)+"\n"
                    +"REPLACING: "+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
                );
        }
    
    }
    

    and in your manifest add it like this (Inside your tag):

    
        
            
            
        
    
    

    When you use a receiver like this you do not call any register or unregister so it will always be ready to get data.

    A note is that this will not work if you let the users move your app to the SD card. If an event is sent when the SD card is unmounted the receiver will not be accessible and you will miss the event.

提交回复
热议问题