2

これが初心者の質問である場合は、すみません。私は、5つの編集テキストから毎日5つのアラームを設定できるすべての可能性を試しました。しかし、何も機能しませんでした!これらの編集テキストを更新するボタン(このコードには表示されていません)もあります(したがって、アラーム時間も更新する必要があります)。これが私のコードです:

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;
            }
        }

PS:fajr、zuhr、asr、maghrib、ishaは5つのEditTextです。保留中のインテントとアラームマネージャーを使用してアラームを起動しようとしましたが、機能しませんでした。誰か良い提案がありますか?

4

3 に答える 3

5

まず、各アラームの保留中のインテントを宣言する必要があります。したがって、5つのアラームが必要な場合は、5回実行する必要があります

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

また、intent_codeも変更する必要があります。新しいコードを登録するたびに、異なるコードを使用する必要があります。私のアプリケーションでは、実行されるたびに乱数が生成されます。インテントを使用して、ここで通知トラフにデータを渡すこともできます。Intentと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);

その後、アラームを登録する必要があります。再度5回、トリガーするアラームごとに1回。

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

アラームを受信して​​通知を表示するには、BroadcastReceiverが必要です。クラス全体を貼り付けています。これは、Time_in_milis_from_now_till_your_alarmで設定された時間の後にトリガーされます。そして、あなたはここであなたが好きなものをほとんど走らせることができます。どんな種類のアラームが欲しいのかわかりません。私の場合は通知を使用しています。通知の詳細については、こちらこちらをご覧ください。

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);
     }

}

最後に、マニフェストでブロードキャストを宣言することを忘れないでください。そうしないと機能しません。これはアプリケーションタグ内に入ります。

アラームマネージャのメソッドに問題がある場合は、ここにドキュメントがあります。

ではごきげんよう!

編集

サウンドを再生するには、開発者ガイドの推奨に従ってMediaPlayerを使用します。mysound.mp3ファイルを/res/rawフォルダーに保存します。そして、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();
   }
于 2012-08-11T11:35:36.463 に答える
1

AsyncTask@Rafael_Tが提案したように、これを実行しようとしているように見えます。あなたがする必要があるのはAlarmManager、あなたが処理するためにアラームが鳴ったときにそれを使用してブロードキャストを送信させることです。これはそれを行う方法についての良いチュートリアルです

于 2012-07-25T23:08:27.957 に答える
0

私はいくつかの同様のアプリケーションを開発しました。

私はこれを解決するためにAlarmMangerを使用しました、そしてそれはうまくいきました。起床日を設定するために、カレンダーオブジェクトを作成しました。翌日のアラームを設定したい場合は、24時間を追加する必要があります。

これはあなたが時間を得る方法です:

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);
        }

そして、これが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);
于 2012-08-06T00:34:37.407 に答える