1

我在 android 中使用 Handler 进行启动通知,在我的代码中我编写 Handler 并分钟转换为 msec 以进行运行服务。但是当时间为 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