1

私はアンドロイドで通知を開始するためにハンドラーを使用します。私のコードでは、ハンドラーを記述し、サービスを実行するために分をミリ秒に変換します。しかし、時間が 2 分で 120000 に変換すると、ハンドラーは 1 分ごとに 1 で実行されます。

私のハンドラ:

private void check_Notify() {
final Handler handler = new Handler();
final Thread r = new Thread() {
    public void run() {

        SharedPreferences preferences = getSharedPreferences("min", 0);

        String min = preferences.getString("minute", "");

        if (min.length() <= 0) {

            s = 120000; 

        } else {

            s = Long.parseLong(min);
            s = s * 60000;

        }

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        Intent intent = new Intent(MainActivity.this,
                Service_class.class);
        PendingIntent pintent = PendingIntent.getService(
                MainActivity.this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 60000, pintent);





        handler.postDelayed(this, s);
    }
};
r.start();
}

service_class.java: このクラスには、カスタム時間で通知​​を表示するためのコマンドが含まれています。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub



            preferences_status = getSharedPreferences("notify_status", 0);

            String status = preferences_status.getString("status", "");

            if (status.length() <= 0 || status.equalsIgnoreCase("0")) {
                Log.e("Notification Status ", "disable");
            } else {
                Log.e("Notification Status ", "enable");

                if (english==null || english=="") {

                } else {
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.ring);
                    mp.start();

                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    Intent contentIntent = new Intent(this,
                            QuestionActivity.class);
                    contentIntent.putExtra("ID", ID);
                    contentIntent.putExtra("english", english);
                    contentIntent.putExtra("farsi", farsi);
                    contentIntent.putExtra("count", count);
                    contentIntent.putExtra("question", question);
                    Notification notification = new Notification(
                            R.drawable.icon, "یاد آوری",
                            System.currentTimeMillis());
                    notification.setLatestEventInfo(this, english, "",
                            PendingIntent.getActivity(this.getBaseContext(), 0,
                                    contentIntent,
                                    PendingIntent.FLAG_CANCEL_CURRENT));
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
                    notificationManager.notify(SIMPLE_NOTFICATION_ID,
                            notification);

                }



        }

        return START_STICKY;
    }

カスタム時間でハンドラーを確認する方法 (例: 4 分 = 240000 ミリ秒)

ありがとう?

4

0 に答える 0