我正在开发一款类似的应用。我使用广播接收器和服务,并使用alarmmanager进行调度,这样你就不需要太多的开销,比如运行一个必须保持活动状态的计时器。
这是我的主要活动。我用按钮启动序列。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (v == btn_splash) {
initiateAlarm(true);
Toast.makeText(MainActivity.this, " alarm scheduled", Toast.LENGTH_LONG).show();
}
} else if (v == btn_cancel)
{
initiateAlarm(false);
Toast.makeText(MainActivity.this, " alarm stopped", Toast.LENGTH_LONG).show();
}
}
};
}
public void initiateAlarm(Boolean bactive) {
alarmUp = false;
alarmUp = (PendingIntent.getBroadcast(getBaseContext(), 0,
new Intent(this, YourService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
Toast.makeText(this, " before scheduling Alarm is already active", Toast.LENGTH_LONG).show();
} else {
YourReceiver.scheduleAlarms(this, prefDuration, bactive);
Toast.makeText(this, "alarms were just scheduled", Toast.LENGTH_LONG).show();
}
alarmUp = false;
alarmUp = (PendingIntent.getBroadcast(getBaseContext(), 0,
new Intent(this, YourService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
Log.d("myTag", "Alarm is already active");
Toast.makeText(this, " after scheduling Alarm is already active", Toast.LENGTH_LONG).show();
}
}
}
这是您的接收器YourReceiver,它在alarmmanager中安排服务。
public class YourReciever extends BroadcastReceiver {
private static final String TAG = "YourReciever";
@Override
public void onReceive(Context ctxt, Intent i) {
context = ctxt;
scheduleAlarms(ctxt, (long) 3600001, true);
}
static void scheduleAlarms(Context ctxt, Long duration, Boolean bactive) {
Log.e(TAG, " ... scheduleAlarms ");
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(ctxt);
prefDuration = preferences.getLong(PREF_DURATION, 3600000);
Log.e(TAG, " ... onReceive ... duration: " + duration);
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, YourService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + duration, duration, pi);
if (bactive == false) {
mgr.cancel(pi);
}
}
}
最后这是服务类。
public class YourService extends IntentService {
public YourService() {
super("YourService");
Log.e(TAG, " ... YourService");
}
@Override
protected void onHandleIntent(Intent intent) {
}
}
类似这样的内容需要在清单文件中声明。
<service
android:name="com.yourpackagename.YourService"
android:exported="true"/>
希望这有所帮助。
祝您有一个美好的一天。
android.intent.action.BOOT_COMPLETED
,这样每当手机启动时都会启动您的服务。如果您想在应用程序中执行此操作,请尝试使用 onResume 和 onStart 方法。 - Aleksander Lidtke(AlarmManager) context.getSystemService(Context.ALARM_SERVICE)
来设置一个任务,通过其.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME_SENSORS, pendingIntent)
方法递归重复执行(其中pendingIntent
将启动您想要执行的代码批处理)。 - Aleksander Lidtke