Ham*_*our 9 android android-intent android-service android-studio
我已经能够在每天的特定时间开始服务,但我想做同样的事情停止但我不知道如何.
这是我用来使用AlarmManager启动服务的代码.
注意:我是Android dev的新手,因此提供完整的评论代码将受到高度赞赏.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0,
new Intent(getApplicationContext(), Service.class), PendingIntent.FLAG_NO_CREATE);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Run Code Online (Sandbox Code Playgroud)
如果您具有上下文,则可以从任何正在运行的类中停止服务.通过以下步骤,您可以使用Receiver在特定时间停止正在运行的服务.
1. 在应用程序中创建一个WakefulBroadcastReceiver类.在接收操作时检查服务是否正在运行,如果使用上下文运行停止.
public class TestReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equalsIgnoreCase("STOP_TEST_SERVICE")) {
if (isMyServiceRunning(context, TestService.class)) {
Toast.makeText(context,"Service is running!! Stopping...",Toast.LENGTH_LONG).show();
context.stopService(new Intent(context, TestService.class));
}
else {
Toast.makeText(context,"Service not running",Toast.LENGTH_LONG).show();
}
}
}
private boolean isMyServiceRunning(Context context,Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
}
2.在AndroidManifest中注册接收器.
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="STOP_TEST_SERVICE" />
<action android:name="START_TEST_SERVICE" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
3. 使用所需操作创建PendingIntent,然后使用活动类中的AlarmManager设置计划操作.
public void setStopServiceAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
new Intent().setAction("STOP_TEST_SERVICE"), PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarm.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
归档时间: |
|
查看次数: |
6826 次 |
最近记录: |