我设置了一个AlarmManager对象来启动服务。
由AlarmManager启动的服务不会从活动中停止
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); //context = getApplicationContext();
Intent alarmServiceIntent = new Intent(context, AlarmHandlingService.class);
alarmServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
alarmServiceIntent.putExtra("alarmId", _id); //_id is a "long" value
PendingIntent pi = PendingIntent.getService(context, (int)_id, alarmServiceIntent, 0);
am.set(AlarmManager.RTC_WAKEUP, time, pi);
AlarmHandlingService
的onStartCommand(..., ..., ...)
的作品,因为它有工作,当它有工作。没有任何问题:
public int onStartCommand(Intent intent, int flags, int startId)
{
long alarmId = intent.getLongExtra("alarmId", -1);
Intent someActivityIntent = new Intent(this, SomeActivity.class);
someActivityIntent.putExtra("alarmId", alarmId);
someActivityIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(someActivityIntent);
return START_NOT_STICKY;
}
但是,当一个新的活动打开(SomeActivity)和我打电话stopService(...)
,该服务没有停止:
stopService(new Intent(context, AlarmHandlingService.class)); //context = getApplicationContext();
这是申报在清单我的服务类:
<service android:name="***myPackageName***.AlarmHandlingService"></service>
我试图调试,发现只有的android.content.ContextWrapper
CLAS的registerReceiver (BroadcastReceiver receiver, IntentFilter filter)
功能当我拨打stopService
时,s被称为。
你的onDestroy()中有什么? – NetStarter
我对onDestroy无能为力。不是在stopService之后调用吗?并且我的stopService根本没有被调用... @覆盖 public boolean stopService(意图名称) { Log.e(TAG,“Service is stopped”); return super.stopService(name); } – smb
Aaaaaa !!!!谢谢你非常非常,NetStarter :)我将代码从stopService移动到onDestroy,现在它的工作:) – smb