Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to send Broadcast from activity :android

Tags:

android

hi friends i am unable to send Broadcast from one activity to other activity pls 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);



}

}

Receiving side 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"); //log our message value 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 is:

   <?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>
like image 682
shyam Avatar asked Apr 25 '12 09:04

shyam


1 Answers

Since the other activity is not running when u send broadcast u wont receive it. If u want to receive broadcasts even when the activity is not running . Declare it in xml .

Here is the code for you. I 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);

    }

}

Second Activity to display 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();
    }
}

Manifest file

<?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>
like image 63
Pratik Avatar answered Sep 28 '22 16:09

Pratik