Cannot send broadcast from activity: android

Hi friends, I can’t send a broadcast from one activity to another activity, see my code below and help:

      public class SendBroadcast extends Activity {
  public static String BROADCAST_ACTION =  "com.unitedcoders.android.broadcasttest.SHOWTOAST";

/*     }
    });
}



   public void sendBroadcast(){

    Intent broadcast = new Intent("com.unitedcoders.android.broadcasttest.SHOWTOAST");
    this.sendBroadcast(broadcast);
    //startActivity(broadcast);



}

}

Host Code:

    public class ToastDisplay extends Activity {

private BroadcastReceiver mReceiver;

@Override
protected void onResume() {

    // TODO Auto-generated method stub
    super.onResume();
    Log.i("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% msg_for_me");

ENT // String msg_for_me = intent.getStringExtra ("some_msg"); // register the value of our message Log.i ("!!!!!!! InchooTutorial @@@@@@@@ $$$$", "%%%%%%%% msg_for_me");

        }
    };
    //registering our receiver
    this.registerReceiver(mReceiver, intentFilter);
}

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //unregister our receiver
    this.unregisterReceiver(this.mReceiver);
}

}

Manifest.xml:

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

Since other activity is not performed when you send a broadcast, you will not receive it. If you want to receive broadcasts, even if the activity is not running. Declare it in xml.

Here is the code for you. Hope this is what you want.

package com.pdd.Receiver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ReceiverActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b=(Button) findViewById(R.id.button1);
        b.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i =new Intent("com.pdd.receiver.myaction");
        sendBroadcast(i);
    }
}

Receiver class

package com.pdd.Receiver;

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 arg0, Intent arg1) {
        // TODO Auto-generated method stub

        //Intent i=new Intent(MyReceiver.class,Second.class);
        Intent i=new Intent(arg0,Second.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        arg0.startActivity(i);

    }

}

Toast

package com.pdd.Receiver;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class Second extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Toast.makeText(getApplicationContext(), "This is second activity", 5000).show();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pdd.Receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver android:name="com.pdd.Receiver.MyReceiver">
             <intent-filter>
                 <action android:name="com.pdd.receiver.myaction"></action>
             </intent-filter>
         </receiver>
        <activity
            android:name=".ReceiverActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
+6

SendBroadcast , .

ToastDisplay, onResume BroadcastReceiver. , , !

, :

sendStickyBroadcast(Intent)

, , BroadcastReceiver, .

+1

All Articles