[英]How to stop a service that is triggered periodically by the Alarm Manager
我正在嘗試一些關於使用服務運行后台任務的教程。 我還嘗試了服務如何定期觸發操作。 但是,當我觸發定期服務時,即使使用了stopService(intent)
命令,我也無法停止它。 嘗試了各種代碼,但服務仍然不會停止,停止它的唯一方法是卸載應用程序。
我采用了使用BroadcastReceiver
和Service
的方法,也使用了AlarmManager
。
主活動.java:
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;
}
}
}
我的服務.java:
` 公共類 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;
}
}
我的接收器.java:
公共類 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);
}
}
在 AlarmManager 上有一個取消方法。 構建用於啟動它的相同意圖,然后將其傳遞給取消方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.