0

I am trying to broadcast from bc_from to bc_to.
It works fine if I use in Activity bc_to:

registerReceiver(receiver, filter);

It does not work if I define the receiver in the Manifest.

I understand from the docs that since 26 it may be impossible.
So I am looking for any solution that will reach Activity bc_to even if it not running.

Thanks

// package com.yotam17.ori.bc_from;
public class MainActivity extends AppCompatActivity {

    private static final String BC_ACTION = "com.yotam17.ori.bc.Broadcast";

    private void send() {
        Intent intent = new Intent(BC_ACTION);
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
    }

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

        send();
    }
}

And the code for bc_from containes Manifest and two clases:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name="com.yotam17.ori.bc_to.MyReceiver" >
        <intent-filter>
            <action android:name="com.yotam17.ori.bc.Broadcast"/>
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


// package com.yotam17.ori.bc_to;
public class MainActivity extends AppCompatActivity {

    private static final String BC_ACTION = "com.yotam17.ori.bc.Broadcast";

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

        // Register MyReceiver
        IntentFilter filter = new IntentFilter(BC_ACTION);
        MyReceiver receiver = new MyReceiver();
        registerReceiver(receiver, filter); //<<<<<< Does not work w/o this
    }
}


public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Got it!!!" , Toast.LENGTH_SHORT).show();
    }
}
Ori
  • 53
  • 3
  • 8
  • Use an explicit `Intent` (e.g., via [`setComponent()`](https://developer.android.com/reference/android/content/Intent.html#setComponent(android.content.ComponentName))), not an implicit `Intent`. – CommonsWare Dec 23 '17 at 20:46

1 Answers1

0

Thanks!
setComponent() solved it.
I never used setComponent() before but found a detailed example here.


To make it a working example - here is the modified send() code:

private void send() {
    Intent intent = new Intent(BC_ACTION);
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    intent.setComponent(new ComponentName("com.yotam17.ori.bc_to","com.yotam17.ori.bc_to.MyReceiver"));
    sendBroadcast(intent);
}
Ori
  • 53
  • 3
  • 8