skip to Main Content

Hello i tried many times when i click the button it doesn’t show the toast and broadcast receiver

the code attached down there kindly correct my miss takes its run successfully but when click the button nothing happend at all , I tried to edit in activity_main.xml but nothing happend

MainActivity.java

package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    // broadcast a custom intent.

    public void broadcastIntent(View view){
        Intent intent = new Intent();
        intent.setAction("com.myapplication.BOOT_COMPLETED"); sendBroadcast(intent);
    }
}

MyReceiver.java

package com.example.myapplication;

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, "Intent Detected.", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">

        <activity android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="MyReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.myapplication.BOOT_COMPLETED">
                </action>
            </intent-filter>

        </receiver>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example of Broadcast"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tutorials point "
        android:textColor="#ff87ff09"
        android:textSize="30dp"
        android:layout_above="@+id/imageButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:minWidth="48dp"
        android:minHeight="48dp"
        tools:ignore="SpeakableTextPresentCheck" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton"
        android:layout_centerHorizontal="true"
        android:onClick="broadcastIntent"
        android:text="broadcast_intent" />

</RelativeLayout>

2

Answers


  1. You are using a Manifest-declared receiver. This receiver would get invoked outside from other app or with some system events.

    The receiver then becomes a separate entry point into your app which means that the system can start the app and deliver the broadcast if the app is not currently running.

    If you want to trigger this receiver within the app you should register this receiver. In your case inside the MainActivity within onCreate or onResume as follows

    broadcastReceiver = new MyReceiver ()
    Intent filter = new IntentFilter("com.myapplication.BOOT")
    registerReceiver(broadcastReceiver, filter)
    

    Also you need to unregister it within onDestroy or onPause

    unregisterReceiver(broadcastReceiver)
    

    I guess you should use Context-registered receiver for your use case where you want to send a broadcast within the app. Its almost the same except that you don’t need to define it inside the manifest.

    More information here

    Login or Signup to reply.
  2. I hope this could help

    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.BroadcastReceiver;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.ConnectivityManager;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
        BroadcastReceiver br;
        IntentFilter filter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            br = new MyReceiver();
    
            filter= new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    
    
    
        }
    
    
    
        public void broadcastIntent(View view){
            Intent intent = new Intent();
            intent.setAction("com.myapplication.BOOT_COMPLETED");
            sendBroadcast(intent);
            this.registerReceiver(br, filter);
        }
    }
    

    also see this link
    https://developer.android.com/guide/components/broadcasts

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search