[英]How to stop a service that is triggered periodically by the Alarm Manager
I was trying out some tutorials on the use of Services to run background tasks.我正在尝试一些关于使用服务运行后台任务的教程。 Also I tried out how the services can trigger the action periodically.
我还尝试了服务如何定期触发操作。 However when I triggered the periodic service I couldnt stop it even after using the
stopService(intent)
command.但是,当我触发定期服务时,即使使用了
stopService(intent)
命令,我也无法停止它。 Tried a variety of code but the service still won't stop and the only way to stop it was to uninstall the application.尝试了各种代码,但服务仍然不会停止,停止它的唯一方法是卸载应用程序。
I have adopted the method of using a BroadcastReceiver
, Service
and also I made use of the AlarmManager
.我采用了使用
BroadcastReceiver
和Service
的方法,也使用了AlarmManager
。
MainActivity.java:主活动.java:
public class MainActivity extends Activity implements View.OnClickListener
{
public class MainActivity extends Activity implements View.OnClickListener
{
TextView tvResults;
Button startService, stopService;
Calendar cal;
Intent intent;
PendingIntent pintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.btnStartService);
stopService = (Button) findViewById(R.id.btnStopService);
tvResults = (TextView) findViewById(R.id.tvResults);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
AlarmManager alarm;
switch (v.getId()) {
case R.id.btnStartService:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent
.getService(this, 0, intent, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// schedule for every 10 seconds
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
10 * 1000, pintent);
// //other way to do it,not periodically
// Context context = getApplicationContext();
// // use this to start and trigger a service
// Intent i = new Intent(context, MyService.class);
// // potentially add data to the intent
// i.putExtra("Service_Key", "Getting email periodically");
// context.startService(i);
// //end of non periodic
tvResults.setText("Success");
break;
case R.id.btnStopService:
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 1, new Intent(
MainActivity.this, MyService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.cancel(pendingIntent);
// Intent i = new Intent(this, MyService.class);
// if (stopService(i)) {
// tvResults.setText("Stopped service");
// }
break;
}
}
}
MyService.java:我的服务.java:
` public class MyService extends Service { ` 公共类 MyService 扩展服务 {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String primaryEmail = getEmail(getApplicationContext());
Toast.makeText(getApplicationContext(), "primary email: " +primaryEmail, Toast.LENGTH_SHORT).show();
return Service.START_NOT_STICKY;
}
//get primary email
static String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
}
//get google email
private static Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
}
MyReceiver.java:我的接收器.java:
public class MyReceiver extends BroadcastReceiver {公共类 MyReceiver 扩展 BroadcastReceiver {
private static final long REPEAT_TIME=1000*10;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context,MyService.class);
PendingIntent pending= PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
//start 30 seconds after boot has completed
cal.add(Calendar.SECOND, 30);
//fetch after every 10seconds
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
}
}
On AlarmManager there is a cancel method.在 AlarmManager 上有一个取消方法。 Build the same intent you use to start it and then pass it to the cancel method.
构建用于启动它的相同意图,然后将其传递给取消方法。
http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29 http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.