Broadcast receiver not working when app is closed

前端 未结 4 1088
遇见更好的自我
遇见更好的自我 2021-01-06 05:21

So I have two different apps made, one sends a broadcast and another receives it and displays a toast. However, when I close the receiver app the broadcast is no longer rece

相关标签:
4条回答
  • 2021-01-06 05:29

    You can go through below solution;

    Activity.java

    Intent intent=new Intent(MainActivity.this,BroadcastService.class);
    startService(intent);
    

    BroadcastService.java

    public class BroadcastService extends Service {
    
    private static MusicIntentReceiver br_ScreenOffReceiver;
    
    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }
    
    @Override
    public void onCreate()
    {
        registerScreenOffReceiver();
    }
    
    @Override
    public void onDestroy()
    {
    
    }
    
    private void registerScreenOffReceiver()
    {
        br_ScreenOffReceiver = new MusicIntentReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                    int state = intent.getIntExtra("state", -1);
                    switch (state) {
                        case 0:
                            Log.e("AAAAAAAAAA", "Headset is unplugged");
                            break;
                        case 1:
                            Log.e("AAAAAAAAA", "Headset is plugged");
                            break;
                        default:
                            Log.e("AAAAAAAAAAAA", "I have no idea what the headset state is");
                    }
                }
            }
        };
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(br_ScreenOffReceiver, filter);
    }
    
    
    }
    

    Menifest

    <service android:enabled="true" android:name=".BroadcastService" />
    
    0 讨论(0)
  • 2021-01-06 05:41

    First of all you need to use the Service for this functionality to work.

    In the Activity you can start and stop the service by using the below codes.

    // to start a service
    Intent service = new Intent(context, MyBrodcastRecieverService.class);
    context.startService(service);
    
    // to Stop service
    Intent service = new Intent(context, MyBrodcastRecieverService.class);
    context.stopService(service);
    

    Then you can use the below service

    public class MyBrodcastRecieverService extends Service
    {
        private static BroadcastReceiver br_ScreenOffReceiver;
    
        @Override
        public IBinder onBind(Intent arg0)
        {
            return null;
        }
    
        @Override
        public void onCreate()
        {
            registerScreenOffReceiver();
        }
    
        @Override
        public void onDestroy()
        {
            unregisterReceiver(br__ScreenOffReceiver);
            m_ScreenOffReceiver = null;
        }
    
        private void registerScreenOffReceiver()
        {
            br_ScreenOffReceiver = new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    Log.d(TAG, "ACTION_SCREEN_OFF");
                    // do something, e.g. send Intent to main app
                }
            };
            
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
            registerReceiver(br_ScreenOffReceiver, filter);
        }
    }
    
    0 讨论(0)
  • 2021-01-06 05:45

    After registering my BroadcastReceiver (BR) statically in the manifest, applying the proper intent filters, using JobIntentService (and registering it in the manifest) to handle the work that was called from my BR, I was still getting inconsistent results.

    Once all of what I listed above has been done you should be able to send an ADB command to activate your broadcast and process the work in your service even if the app is closed. This was working for me some of the time, but not all of the time.

    This article describes limitation to BRs. "As of Android 3.1 the Android system excludes all receiver from receiving intents by default if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu" (AKA a user executes Force Stop)

    When I start the app by debugging it, then swipe it closed on my device, my ADB command never activates my BR. However, after my debugging session is over, when I open up the app on my device and swipe it closed, I can activate my BR through ADB commands.

    This occurs because when you debug an application, then manually swipe it closed on the device, Android considers this a Force Stop hence why my BR cannot be activated until I re-open the app on the device without debugging.

    Scoured the internet for hours, and wasn't able to find my solution, so I thought I'd post it here just in case some poor unfortunate soul is encountering the same weird functionality I was.

    Happy coding :)

    0 讨论(0)
  • 2021-01-06 05:49

    Try this way..

    Intent i = new Intent();
    i.setAction("com.example.ali.rrr");
    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    i.setComponent(  
            new ComponentName("PackageNameApp2","PackageNameApp2.MainActivity"));  
    sendBroadcast(i);
    
    0 讨论(0)
提交回复
热议问题