简体   繁体   中英

Broadcast Receiver not showing result when broadcasting from another app Activity

I am trying to make broadcast receiver which will show a toast message when user click on button from my another app activity .

But receiver not showing result.

My code is below

My Receiver App

MyReceiver :

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Broadcast has been received!", Toast.LENGTH_LONG).show();
    }
}

Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xyz.receivebroadcast">

    <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=".MyReceiver"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="com.example.xyz.broadcasts"></action>
            </intent-filter>

        </receiver>
    </application>

My sender app

My MainActivity which sends the broadcasts is here.

MainActivity :

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void SendOutBroadcast(View view){
        Intent i = new Intent();
        i.setAction("com.example.xyz.broadcasts");
        i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(i);
    }
}

activity_main.xml :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xyz.broadcasts.MainActivity">

    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="SendOutBroadcast"
        android:text="Send Broadcast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

In your case your activity Intent should be like this:

Intent intent = new Intent();
intent.setAction("com.example.xyz.broadcasts");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(  
        new ComponentName("com.pkg.AppB","com.pkg.AppB.MainActivity"));  
sendBroadcast(intent);

Try this may help you or visit for more information.

NOTE - This may be not going to work in Android O because in Android O implicit broadcast are ban as mentioned Here . (Also credits to you @Muhammad Arslan Maqsood)

i found the issue: issue is not with code, actually Android O disabled the feature "Implicit broasdcast". see this

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