Broadcast receiver not working in android oreo

前端 未结 6 1073
迷失自我
迷失自我 2020-12-03 05:28

My Broadcast receiver is not working on oreo but its working below oreo it\'s working fine, I searched a lot regarding this but could not find the suitable solution. Does an

6条回答
  •  抹茶落季
    2020-12-03 05:59

    I also had this kind of issue, but I found a better solution:

    Class MyReceiver

    @BroadcastReceiverActions({
            "android.intent.action.SCREEN_ON",
            "android.intent.action.SCREEN_OFF",
            "android.intent.action.DREAMING_STARTED",
            "android.intent.action.DREAMING_STOPPED",
            "android.intent.action.ACTION_POWER_DISCONNECTED",
            "android.intent.action.ACTION_POWER_CONNECTED",
            "android.net.conn.CONNECTIVITY_CHANGE"
    })
    public class MyReceiver extends BroadcastReceiver {
    
        public MyReceiver() {
            super();
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Session.getGlobalReceiverCallBack(context, intent);
    
            //Log.e("dfd", "" + intent.getAction());
        }
    }
    

    Class AppController

    public class AppController extends Application {
    
        private BroadcastReceiver receiver;
        MyReceiver mR;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mR = new MyReceiver();
            receiver = DynamicReceiver.with(mR)
                    .register(this);
    
        }
    }
    

    Class MainActivity

    public class MainActivity extends AppCompatActivity implements GlobalReceiverCallBack {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Session.setmGlobalReceiverCallback(this);
    
        }
    
        @Override
        public void onCallBackReceived(Context context, Intent intent) {
    
            Toast.makeText(context, "" + intent.getAction(), Toast.LENGTH_LONG).show();
        }
    }
    

    For complete reference you can see also https://github.com/devggaurav/BroadcastReceiver-For-Naught-and-Oreo-devices

提交回复
热议问题