简体   繁体   中英

Data send from service to activity not working

I am calling a function in service to send data to activity, i am using Local broadcast manager for this, the below is the code in service

    private void sendDataToActivity(List<WNotificationModel> notificationList) {
    Intent intent=new Intent("listDataUpdate");
    intent.putExtra("hello","helloWorld");
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

the below is the code for receiving data in activity

public class WNotificationList extends AppCompatActivity {

private BroadcastReceiver mMessageReceiver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String name=intent.getStringExtra("hello");
        Log.d("catAndDog", "onReceive: "+name);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_where_am_ilist_notification);

}

@Override
protected void onStart() {
    super.onStart();
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mMessageReceiver,new IntentFilter("listDataUpdate"));
}

@Override
protected void onStop() {
    super.onStop();
    LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mMessageReceiver);
}

}

There are some updates in broadcast.

Beginning with Android 9 (API level 28), The NETWORK_STATE_CHANGED_ACTION broadcast doesn't receive information about the user's location or personally identifiable data.

In addition, if your app is installed on a device running Android 9 or higher, system broadcasts from Wi-Fi don't contain SSIDs, BSSIDs, connection information, or scan results. To get this information, call getConnectionInfo() instead.

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.

If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.

Please have a look inDeveloper.android.com

Let see if this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM