如何使用警报管理器启动intent服务

时间:2014-04-24 05:03:02

标签: android android-intent

我想通过闹钟管理器启动我的意向服务,我的意图服务正在通过服务启动..但我无法做到这一点请帮助我这样做 这是我的代码 服务代码

public void onCreate() {

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

        startService(intent);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                10 * 1000, pintent);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 10 * 1000, pintent);

    }


    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast t = Toast.makeText(getApplicationContext(), "in service onSatrt commandf ", Toast.LENGTH_LONG);
        t.show();

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

        startService(intent);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                10 * 1000, pintent);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 10 * 1000, pintent);
        return super.onStartCommand(intent, flags, startId);
    }

    public IBinder onBind(Intent arg0) {

        return null;
    }

1 个答案:

答案 0 :(得分:0)

试试这个

您需要使用PendingIntent.FLAG_UPDATE_CURRENT

将您的代码从PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);更改为

PendingIntent pintent = PendingIntent.getService(this,
                        0,  serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);