public class SchedulerEventService extends Service {
private static final String APP_TAG = "com.hascode.android.scheduler";
Context contxt;
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
Log.d(APP_TAG, "event received in service: " + new Date().toString());
Toast.makeText(contxt, "Fire", 1000).show();
return Service.START_NOT_STICKY;
}
}
public class SchedulerSetupReceiver extends BroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";
private static final int EXEC_INTERVAL = 20 * 1000;
@Override
public void onReceive(final Context ctx, final Intent intent) {
Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called");
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit
// intent
PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, 20);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
Toast.makeText(ctx, "Fire", 1000).show();
}
}
public class SchedulerEventReceiver extends BroadcastReceiver {
private static final String APP_TAG = "com.hascode.android";
@Override
public void onReceive(final Context ctx, final Intent intent) {
Log.d(APP_TAG, "SchedulerEventReceiver.onReceive() called");
Intent eventService = new Intent(ctx, SchedulerEventService.class);
ctx.startService(eventService);
Toast.makeText(ctx, "Fire", 1000).show();
}
}
<service
android:name="com.example.tataalarmmanger.SchedulerEventService"
android:process=":hascode_process" >
</service>
<receiver
android:name="com.example.tataalarmmanger.SchedulerEventReceiver"
android:process=":hascode_process" >
</receiver>
<receiver
android:name="com.example.tataalarmmanger.SchedulerSetupReceiver"
android:process=":hascode_process" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
这是我的代码,当我运行此代码我无法启动服务我想安排警报动态时间来自服务我已设定时间,它应该反复点火警报每20秒我有为了得到时间而没有得到约会,请告诉我在哪里做错了。
答案 0 :(得分:0)
扩展WakefulBroadcastReceiver而不是简单的BroadcastReceiver
答案 1 :(得分:0)
尝试此类扩展服务
public class SchedulerEventService extends Service {
private static final String APP_TAG = "com.hascode.android.scheduler";
Context contxt;
private static final String APP_TAG = "com.hascode.android";
private final long EXEC_INTERVAL = 20 * 1000;
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, 20);
alarmManager.setExactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), EXEC_INTERVAL, intentExecuted);
Log.d(APP_TAG, "event received in service: " + new Date().toString());
Toast.makeText(contxt, "Fire", 1000).show();
return Service.START_STICKY;
}
}