這裏是我的代碼啓動服務:無法停止服務?
我開始我的服務onCreate()我的活動。
Intent svc=new Intent(BeapSoundActivity.this, BackgroundService.class);
pendingIntent = PendingIntent.getService(BeapSoundActivity.this, 0, svc, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
我不能能夠通過調用
stopService停止服務();
這裏是我的服務類:
public class BackgroundService extends Service{
// private static final String TAG = null;
// MediaPlayer player;
MediaPlayer mMediaPlayer;
String strResponse;
int i=0;
static {
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
mMediaPlayer = new MediaPlayer();
Log.e("Oncreate","oncreate");
}
public int onStartCommand(Intent intent, int flags, int startId) {
// player.start();
Log.e("Onstartcommand","onstartcommand");
pendingCount();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
// super.onStart(intent,startId);
Log.e("Onstart","onstart");
// pendingCount();
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
this.stopSelf();
}
public void onPause() {
}
@Override
public void onDestroy() {
super.onDestroy();
this.stopSelf();
// player.stop();
// player.release();
}
@Override
public void onLowMemory() {
}
}
public void pendingCount(){
my operations during the service
}
}
}
我也試圖與stopSelf()在我的onDestroy(),但它也didnt爲我工作。
請問我該如何解決這個問題?
你需要靜態塊。 –
它只是有些時候我無法使用我的web服務,我不認爲這是問題,stopService()調用onDestroy方法,我保留this.stopSelf(),但它沒有工作 – user2717010
是你做任何計數器操作使用匿名類或任何其他意思內pendingCount()...如果是的話,我想我知道解決方案 –