12

I've been struggling with this for hours. I've also checked the documentation and several topics. I found this code in two topics, both guys said the code was working perfectly, but not on my computer. The first Toast appears, but the second one never. What is wrong?

public class HelloAndroid2 extends Activity {  


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

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

 public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

}

Tim
  • 41,901
  • 18
  • 127
  • 145
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Please show your AndroidManifest.xml. You should declare your broadcast receiver in the manifest and the action it would react to – ccheneson Jan 11 '11 at 18:05

3 Answers3

39

Actually you dont need to specify the action since you use the class AlarmReceiver.class in the intent.

In your AndroidManifest.xml, make sure you have a receiver definition within the <application> tags, something like:

<receiver android:name="AlarmReceiver">

Edit: Ok there are 2 ways to use your broadcast receiver.

1) From the code you have provided, AlarmReceiver.java that will contains:

public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

and HelloAndroid2.java:

public class HelloAndroid2 extends Activity {  


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

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

}

Like this, you can set your broadcast receiver to work with the AndroidManifest.xml and the tag <receiver ...>

2)2nd way. With this way, you can use just 1 file HelloWorld2.java:

In your activity, create your broadcast receiver and register it.

public class HelloWorld2 extends Activity {
    private SharedPreferences prefs;
    private String mName;


    BroadcastReceiver alarmReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();          
        }
    };


    public static final String ACTION_NAME = "com.helloworld.MYACTION";
    private IntentFilter myFilter = new IntentFilter(ACTION_NAME);


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

    @Override
    protected void onResume() {
        registerReceiver(alarmReceiver, myFilter);
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        registerReceiver(alarmReceiver, myFilter);

        Intent intent = new Intent(ACTION_NAME);        

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();


    }
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • Thanks, for the answer, but still nothing. This is the .xml file: [/code] – erdomester Jan 11 '11 at 19:14
  • 1
    You should add this to your post and post something like "Edit", so that people knows that it s an additional information – ccheneson Jan 11 '11 at 19:25
  • Do you see at least the first toast message "Alarm set"? – ccheneson Jan 11 '11 at 19:58
  • Ok, the code you have posted. Is it in 2 files : HelloAndroid2.java and AlarmReceiver.java ? – ccheneson Jan 11 '11 at 20:22
  • 1 file. Only the package and the import rows are missing from the code, so as a matter of fact, this is the whole code. I have also tried android:enabled="true" – erdomester Jan 11 '11 at 20:34
  • Both solutions work perfectly. It's really weird, that if i put my code into two files it works, but in file it doesn't. I've uploaded my file here, could you check if something is missing from it? http://www.2shared.com/file/cqXGQgxQ/HelloAndroid2.html – erdomester Jan 11 '11 at 23:09
  • 1
    It's because, if you embedded a broadcast receiver in an activity, you need to register it to the activity. More information at http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter). – ccheneson Jan 12 '11 at 18:22
  • Thanks for the post, adding the receiver to the manifest fixed my code. – Stuntbeaver Mar 12 '11 at 10:27
  • 1
    AH....... I can't tell you how much time I spent trying to figure this out. The manifest! Thanks! – woot Jan 29 '19 at 02:43
11

I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.

Simply changed:

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

for:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
Sebastian Bruno
  • 187
  • 1
  • 4
0

If the answer above doesn't work for you then there is another way to not receive any callbacks when AlarmManager fires an expired alarm. You simply need to check this one out: by sending the wrong Intent on instantiation of PendingIntent. For example you wanted to receive a call onReceive on one of your receivers but you instantiated a PendingIntent via getActivity or getService, but what you actually meant is getReceiver.

When creating instance of PendingIntent, there are many ways to create it (getService, getActivity,getReceiver, getForegroundService:

if you want Activity the receiver of the intent then you:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);

if you want BroadcastReceiver the receiver of the intent:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);

if you want a foreground Service the receiver of the intent:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);

if you want a Service the receiver of the intent:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

I also posted similar answer here.

HTH

Neon Warge
  • 1,817
  • 6
  • 29
  • 53