BroadcastReceiver not receiving broadcast

I am trying to convey a message with a toast with the following extension code Activity. But the broadcast is not accepted by others Activity, the toast is not displayed. Can someone solve my mistake? Main action - SendBroadcast.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SendBroadcast extends Activity {

    public static String BROADCAST_ACTION =
                             "com.unitedcoders.android.broadcasttest.SHOWTOAST";

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

    public void sendBroadcast(View v) {
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        sendBroadcast(broadcast);
    }
}

Display Activity Toast ToastDisplay.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;

public class ToastDisplay extends Activity {

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "received",
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onResume() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SendBroadcast.BROADCAST_ACTION);
        registerReceiver(receiver, filter);
        super.onResume();
    }

    @Override
    protected void onPause() {
        unregisterReceiver(receiver);
        super.onPause();
    }
}

and manifest.java is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.broad"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SendBroadcast"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".ToastReceiver" >
            <intent-filter>
                <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
+5
source share
4 answers

There can be two types of brookast: static and dynamic. Static are those declared in the manifest file. Dynamic can be declared at run time. You combined these two types of translation in one translation.

, ( ). .

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BroadcastTestActivity extends Activity {

    public static String BROADCAST_ACTION =     
                            "com.unitedcoders.android.broadcasttest.SHOWTOAST";
    BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.w("Check", "Inside On Receiver");
            Toast.makeText(getApplicationContext(), "received",
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(br, filter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sendBroadcast();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(br);
    }

    public void sendBroadcast() {
        Intent broadcast = new Intent();
        broadcast.setAction(BROADCAST_ACTION);
        broadcast.addCategory(Intent.CATEGORY_DEFAULT);
        sendBroadcast(broadcast);
    }
}

, , , . ( ).

+9

ToastReceiver ?

<receiver android:name=".ToastReceiver">
    <intent-filter>
        <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>`

public class ToastDisplay extends Activity {
    private BroadcastReceiver receiver = new BroadcastReceiver() {

}

to

public class ToastReceiver extends BroadcastReceiver {

}
+3

Try

<activity android:name=".SendBroadcast" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.BROADCAST" />
    </intent-filter>
</activity>    

Into the onCreate()challenge

sendBroadcast(v);
0
source
Button b1 = (Button)findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        try {
            String RESULT = new TestAsyncTask().execute(" ").get();
            System.out.println("RESULT "+RESULT);
        } 
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
-2
source

All Articles