Frage

Please excuse me if this is a noob question. I have tried every possibility I could to set five alarms daily from the five edit texts. But nothing worked! I also have a button (not shown in this code) which updates these edit texts (therefore should update the alarm times as well). Here's my code:

for (int i = 0; i < 5; i++) {
            switch (i) {
            case 0:
                fajr.setText(result[i]);
                tFajr = new GregorianCalendar();
                tFajr.set(year, month, day,
                        Integer.parseInt(result[i].substring(0, 2)),
                        Integer.parseInt(result[i].substring(3, 5)));
                break;
            case 1:
                zuhr.setText(result[i]);
                tZuhr = new GregorianCalendar();
                tZuhr.set(year, month, day,
                        Integer.parseInt(result[i].substring(0, 2)),
                        Integer.parseInt(result[i].substring(3, 5)));
                break;
            case 2:
                asr.setText(result[i]);
                tAsr = new GregorianCalendar();
                tAsr.set(year, month, day,
                        Integer.parseInt(result[i].substring(0, 2)),
                        Integer.parseInt(result[i].substring(3, 5)));
                break;
            case 3:
                maghrib.setText(result[i]);
                tMaghrib = new GregorianCalendar();
                tMaghrib.set(year, month, day,
                        Integer.parseInt(result[i].substring(0, 2)),
                        Integer.parseInt(result[i].substring(3, 5)));
                break;
            case 4:
                isha.setText(result[i]);
                tIsha = new GregorianCalendar();
                tIsha.set(year, month, day,
                        Integer.parseInt(result[i].substring(0, 2)),
                        Integer.parseInt(result[i].substring(3, 5)));
                break;
            }
        }

P.S: fajr,zuhr,asr,maghrib,isha are the five EditTexts. I tried to use a pending intent and an alarm manager to fire the alarms but it didnt work. Does any one have a good suggestion?

War es hilfreich?

Lösung

First of all you need to declare a pending intent for each of the alarms. So if you want 5 alarms you will need to run it 5 times

PendingIntent sender = PendingIntent.getBroadcast(context,intent_code, intent, 0);

and the intent_code should change as well. Every time you register a new one you have to use a different code. In my application I have a random number generated every time that is executed. You can also pass data to your notification trough here using the Intent. Mind the difference between Intent and PendingIntent.

Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("title", "some title");
intent.putExtra("notes","some notes");
Random r = new Random();
intent_code = r.nextInt();
PendingIntent sender = PendingIntent.getBroadcast(context,intent_code, intent, 0);

After that you need to register your alarm. Again 5 times, one for each alarm you want to trigger.

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, Time_in_milis_from_now_till_your_alarm, sender);

You need a BroadcastReceiver to receive your alarm and display the notification. I am pasting my whole class. This will be triggered after the time set in the Time_in_milis_from_now_till_your_alarm. And you can run pretty much whatever you like in here. I don't know what kind of alarm you want, in my case I'm using a notification. You can find the details about the notification here and here.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
     public void onReceive(Context context, Intent intent) {
        Log.d("receiver", "received");
        NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Bundle b = intent.getExtras();


        int icon = R.drawable.icon; // icon from resources
        CharSequence tickerText = b.getString("title"); ; // ticker-text
        long when = System.currentTimeMillis(); // notification time
        CharSequence contentText = b.getString("notes");; // message text
        Toast.makeText(context, tickerText, Toast.LENGTH_SHORT).show();

        Intent notificationIntent = new Intent(context, AppDelegate.class);

        // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 
                                                                0, 
                                                                notificationIntent,
                                                                Intent.FLAG_ACTIVITY_NEW_TASK);

        // the next two lines initialize the Notification, using the
        // configurations above
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context,tickerText, contentText, contentIntent);

        mManager.notify(12, notification);
     }

}

Last, don't forget to declare your broadcast in the manifest or it wont work. This goes inside application tag.

If you have any problems with the alarm manager methods you can find the documentation here.

All the best!

Edit

To play a sound use the MediaPlayer as recomended by the developer's guide. Keep the file mysound.mp3 in your folder /res/raw. And you just call the following method in your BroadcastReceiver!

public void playSound() {                
     MediaPlayer sound = MediaPlayer.create(this, R.raw.mysound);
     sound.setOnCompletionListener(new OnCompletionListener() {
     @Override
     public void onCompletion(MediaPlayer mp) {
            mp.release();
     }

     });

    quadrantChangeSound.start();
   }

Andere Tipps

It looks to me like you're trying to do this in an AsyncTask as @Rafael_T suggested. What you need to do is use AlarmManager and have it send a broadcast when the alarms go off for you to handle. Here is a good tutorial on how to do it.

I developed some similar application.

I used an AlarmManger to solve this and it worked fine. To set the wakeup date I created an calendar object. In case you want to setup the alarm for the next day, you have to add 24 hours.

This is how you get the time:

Calendar calNew = Calendar.getInstance();
        calNew.set(Calendar.HOUR_OF_DAY, hour);
        calNew.set(Calendar.MINUTE, minute);
        calNew.set(Calendar.SECOND, 0);

        Calendar calNow = Calendar.getInstance();
        // Get the AlarmManager service
        if (calNew.getTimeInMillis() < calNow.getTimeInMillis()) {
            calNew.add(Calendar.HOUR_OF_DAY, 24);
        }

And this is how you setup the AlarmManager:

// INTENT AND ALARMMANAGER PLANING
    Intent intent = new Intent(context, AlarmReceiver.class);

    sender = PendingIntent.getBroadcast(context, INTENT_CODE, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, calNew.getTimeInMillis(), sender);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top