Broadcast receiver not working? - android

I m using a broadcast receiver with multiple intent filters. My requirement is to access the click of notification and make a job with that click in background(if app minimised) or foreground. The first two jobs are finishing, but the third one not executing. Where i m wrong ?
Here is my code
public class MainActivity extends Activity{
int choice; EditText et;
#override
onCreate(Bundle SavedInstanceState){
setContentView(R.layout.main);
et= (EditText)findViewById(R.id.choice);
choice=Integer.parseInt(et.getText().toString());
//some_stuff
if (choice==1)
registerReceiver(br, new IntentFilter("ACT_ONE");
else if (choice==2){
registerReceiver(br, new IntentFilter("ACT_TWO");
registerReceiver(br, new IntentFilter("ACT_THREE");
}
else if (choice==3) {
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
nb.setContentTitle("Do Job_Four");
nb.setContentText("Click here to do job four");
nb.setAutoCancel(true);
nb.setSmallIcon(R.drawable.icon);
nb.setDefaults(Notification.DEFAULT_ALL);
Intent in=new Intent(this,MainActivity.class);
IntentFilter lf=new IntentFilter("ACT_FOUR");
PendingIntent pi=PendingIntent.getBroadcast(this,(int)System.currentTimeMillis(),in,0);
nb.setContentIntent(pi);
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
registerReceiver(br,lf);
nm.notify((int)System.currentTimeMillis(),nb.build());
}
}
private void m_one(){
//job_one_code
}
private void m_two(){
//job_two_code
}
private void m_three(){
//job_three_code
}
BroadcastReceiver br=new BroadcastReceiver(){
#override
onReceive(Context c, Intent i){
if(i.getAction().equals("ACT_ONE")
m_one();
else if(i.getAction().equals("ACT_TWO")
m_two();
else if(i.getAction().equals("ACT_THREE")
m_two();
else if(i.getAction().equals("ACT_FOUR")
m_three();
}
};
#override
public void onDestroy(){
unregisterReceiver(br);
super.onDestroy();
}
}
Here is manifest
<receiver android:name=".MainActivity">
<intent-filter android:priority="999">
<action android:name="ACT_ONE"/>
</intent-filter>
</receiver>

my pseudo code:-- Please register onPause() and unregister
on onResume() method it will work.
public class AndroidBroadcast extends Activity {
private BroadcastReceiver the_receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context c, Intent i) {
}
};
// Set When broadcast event will fire.
private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction("ax.chayan.mybroadcast");
sendBroadcast(intent);
}
#Override
protected void onResume() {
// Register reciever if activity is in front
this.registerReceiver(the_receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// Unregister reciever if activity is not in front
this.unregisterReceiver(the_receiver);
super.onPause();
}
}

Related

Broadcast Receiver "on recieve" method not been called?

I Have three activities
On activity A i register the broadcast receiver ,then i go to activity B from there i go to activity C.
and finally onBackPressed of activity c ,i send the broadcast
but onReceive is not called
My first Activity
private MyBroadCastReceiver myRecevier = new MyBroadCastReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frag=new Intent(MainActivity.this,Activity2.class);
//frag.putExtra("Limit", foo);
startActivity(frag);
// }
}
});
}
#Override
protected void onResume() {
super.onResume();
//Register the activity to the broadcast receiver
registerReceiver(myRecevier, new IntentFilter(MyBroadCastReceiver.ACTION));
}
#Override
protected void onPause() {
super.onPause();
//Unregister the activity from the broadcast receiver. Good practice ;)
unregisterReceiver(myRecevier);
}
public class MyBroadCastReceiver extends BroadcastReceiver{
public static final String ACTION = "com.uberrueco.mybroadcastreceiver.receivers";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("MyBroadCastReceiver", "received");
Toast.makeText(context,"Received "+intent.getStringExtra("editText"), Toast.LENGTH_LONG).show();
}
}
}
Second activity has nothing but an intent to activity 3
Third Activity
public class Activity3 extends Activity {
EditText etReceivedBroadcast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3);
etReceivedBroadcast = (EditText) findViewById(R.id.etReceivedBroadcast);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent intent = new Intent(this, MyIntentService.class);
intent.putExtra("editText", etReceivedBroadcast.getText().toString());
startService(intent);
}
}
and finally my IntentService class
public class MyIntentService extends IntentService{
public MyIntentService(){
super("MyIntentService");
}
public MyIntentService(String name) {
super(name);
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("MyIntentService", "handling intent...");
//Intent created for broadcasting
Intent intentBroadCast = new Intent();
//Filter the broadcast to the action desired
intentBroadCast.setAction(MyBroadCastReceiver.ACTION);
intentBroadCast.putExtra("editText", intent.getStringExtra("editText"));
//Send the broadcast :D
sendBroadcast(intentBroadCast);
}
}
You are calling unregisterReceiver in onPause of MainActivity . So you are not recieving the broadcast.
Move register to onCreate and unregister to onDestroy of your MainActivity.
if your onHandleIntent() was called then you should try like.
Intent intentBroadCast = new Intent(MyBroadCastReceiver.ACTION);
intentBroadCast.putExtra("editText", intent.getStringExtra("editText"));
//Send the broadcast :D
sendBroadcast(intentBroadCast);
make changes like below code
private MyBroadCastReceiver myRecevier = new MyBroadCastReceiver();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register the activity to the broadcast receiver
this.registerReceiver(myRecevier, new IntentFilter(MyBroadCastReceiver.ACTION));
Submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent frag=new Intent(MainActivity.this,Activity2.class);
//frag.putExtra("Limit", foo);
startActivity(frag);
// }
}
});
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onDestroy() {
super.onDestroy();
//Unregister the activity from the broadcast receiver. Good practice ;)
this.unregisterReceiver(myRecevier);
}
#Override
protected void onPause() {
super.onPause();
//Unregister the activity from the broadcast receiver. Good practice ;)
unregisterReceiver(myRecevier);
}
public class MyBroadCastReceiver extends BroadcastReceiver{
public static final String ACTION = "com.uberrueco.mybroadcastreceiver.receivers";
#Override
public void onReceive(Context context, Intent intent) {
Log.d("MyBroadCastReceiver", "received");
Toast.makeText(context,"Received "+intent.getStringExtra("editText"), Toast.LENGTH_LONG).show();
}
}
}
u need to use like this this.unregisterReceiver() and this.registerReceiver()

LocalBroadcastManager Broadcasting not being receieved

I've tried the examples from many different answers to similar questions, but nothing works. When I press the back button, I get "Broadcasting message" in the log but not "Broadcast received". Why?
public class MainActivity extends ActionBarActivity
implements ContactsFragment.ContactListener {
private IntentFilter filter;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
}
};
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
filter = new IntentFilter(Constants.BROADCAST_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
}
#Override
public void onBackPressed() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent(Constants.BROADCAST_ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
if (!pane.isOpen()) {
pane.openPane();
} else {
finish();
}
}
#Override
public void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
super.onPause();
}
}

Use IntentFiler in onPause()

I want to start my Application (ex update my TextView) after press back button. But it didn't work. Please help me.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
registerReceiver(intentReceiver, intentFilter);
}
protected void onPause() {
registerReceiver(intentReceiver, intentFilter);
super.onPause();
}
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
sendSMS("123","message");
}
};
I have updated your code try this and if any problem is there then ask me. I will try to help you ..
#Override
public void onBackPressed() {
// FOR UPDATING YOUR TEXT VIEW.
Intent broadcast_intent = new Intent("SMS_RECEIVED_ACTION");
broadcast_intent.putExtra("number", textview_value_to_set);
sendBroadcast(broadcast_intent);
// // FOR STARTING YOUR NEW ACTIVITY.
Intent intent = new Intent(getApplicationContext(), class_name.class);
startActivity(intent);
super.onBackPressed();
}
Use this as your broadcast receiver. Dont forget to register reciever.
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("SMS_RECEIVED_ACTION")) {
TextView text = (TextView)findViewById(R.id.text);
text.setText(intent.getExtras().getString("number"));
}
}
};

Service to Activity communication

I am trying to update the UI (Activity) after some action has been performed in the service. This is very simple example but it doesn't seem to work for me. What am I missing here?
ExampleService:
public class ExampleService extends IntentService{
#Override
protected void onHandleIntent(Intent intent) {
notifyActivity();
}
private void notifyActivity() {
Intent broadcast = new Intent(this, ExampleActivity.class);
sendBroadcast(broadcast);
}
}
ExampleActivity:
public class ExampleActivity extends ListActivity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
#Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
registerReceiver(receiver, filter);
}
}
You cannot send a broadcast to an anonymous dynamic receiver that way. You will need to define an action string in the Intent and use that action string in the IntentFilter.
You might consider using LocalBroadcastManager for this scenario, for better performance. Here is a sample project demonstrating this.

Android BroadcastReceiver Register Problem

I am trying to register a broadcastreceiver in a small Android app that I am playing around with. I think I am doing what I am supposed to do, but still the receiver doesn't register. It must be something small that I am missing and it is driving me crazy.
public class CreateReceiver extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}//end of onCreate
BroadcastReceiver myreceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context recvc, Intent recvi)
{
}
};
#Override
public void onResume()
{
super.onResume();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
if (this.registerReceiver(myreceiver, intentFilter) == null)
{
Toast.makeText(this, "Could not register receiver", Toast.LENGTH_LONG).show();
}//end of if
}//end of onResume
#Override
public void onPause()
{
this.unregisterReceiver(myreceiver);
super.onPause();
}//end of onPause
}//end of CreateReceiver
Can anyone tell me what I am doing wrong? Thanks.
You have to register your BroadcastReceiver from within onCreate
public class CreateReceiver extends Activity
{
private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(myreceiver , intentFilter);
}//end of onCreate
BroadcastReceiver myreceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context recvc, Intent recvi)
{
}
};
...

Categories

Resources