我在我的项目中做了一项服务,我想每 2 分钟开始一次。我正在使用以下代码,但它无法正常工作。
public class ScheduleSync extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartMyServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 60000*2, pending);
}
}
public class StartMyServiceReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "receive broadcast", Toast.LENGTH_LONG).show();
Log.d("StartMyServiceReceiver", "receive broadcast");
Intent service = new Intent(context, myservice.class);
context.startService(service);
}
}
请帮助我解决这个问题。当我看到 taskmanager myservice 在后端运行时。