2013-09-16 98 views
1

我有活動,即啓動/停止服務。還有一個服務,它有一個循環。我目前正試圖阻止我的主要活動的服務。我嘗試了很多變體,但都沒有調用onDestroy:/。任何幫助,想法或教導將不勝感激。 :)從我的主要活動從活動中停止服務循環

部分,我嘗試用對話來停止服務:

private void AlertDialog() { 
     new AlertDialog.Builder(this) 
     .setTitle("Delete entry") 
     .setMessage("Are you sure?") 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

        stopService(intent); 
      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // do nothing 
      } 
     }) 
     .show(); 

服務:

public class SensMessageService extends Service{ 

public final String APP = "Ultimator"; 
public final String PREF_IN_USE = "inuse"; 
public final String PREF_TOTAL_MESSAGES = "totalmes"; 
public final String PREF_LEFT_MESSAGES = "leftmes"; 
public final String MESSAGE_BODY = "sms_body"; 
public final String MESSAGE_RECEIVER = "sms_receiver"; 
public final String MESSAGE_REPEATS = "sms_repeats"; 
public final String TAG = "SensMessageService"; 

private IBinder ibinder; 

private SharedPreferences prefrences; 

@Override 
public IBinder onBind(Intent arg0) { 
    return this.ibinder; 
} 


public class LocalBinder extends Binder{ 

    SensMessageService getBinder(){ 
     return SensMessageService.this; 
    } 

} 

@Override 
public boolean onUnbind(Intent intent) { 
    // TODO Auto-generated method stub 
    return super.onUnbind(intent); 
} 

@Override 
public void unbindService(ServiceConnection conn) { 
    // TODO Auto-generated method stub 
    super.unbindService(conn); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    return super.onStartCommand(intent, flags, startId); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    stopSelf(); 
    Toast.makeText(getApplicationContext(), "Stopped", Toast.LENGTH_LONG).show(); 

} 

@Override 
public void onStart(Intent intentas, int startId) { 
    final Intent intent = intentas; 

      SmsManager smsManager = SmsManager.getDefault(); 
      int repeat = Integer.parseInt(intent.getStringExtra(MESSAGE_REPEATS)); 
      String sendTo = intent.getStringExtra(MESSAGE_RECEIVER); 
      String myMessage = intent.getStringExtra(MESSAGE_BODY); 

      for (int i=0; i<repeat; i++) { 

       smsManager.sendTextMessage(sendTo, null, myMessage, null, null); 
      } 

    super.onStart(intent, startId); 
} 

}

我想是的onDestroy不被稱爲:/,因爲我沒有看到烤麪包

UPDATE:

我添加了一個線程,但不知何故,因爲你使用getApplicationContext()它不會打印出

@Override 
        public void run() { 
         try { 
          while(true) { 
           sleep(1000); 
           System.out.println("fff"); 
          } 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       }; 

       thread.start(); 
+1

也請發表您的服務代碼。 –

+1

也從'onDestroy'中刪除'stopself'。在onDestroy中意味着服務已經停止。所以調用'stopself'不起作用。 – Sunny

+0

它看起來像onStart循環將花費幾分之一秒完成。您確定服務尚未停止(完成)到您嘗試阻止您的活動時? – Tenfour04

回答

2

首先,onStart()已被棄用約四年。請使用onStartCommand()。第二,請在後臺線程中做你的短信工作,而不是你現在正在做的主應用程序線程。

三,請勿在onDestroy()中撥打stopSelf()。如果您的服務達到onDestroy(),則不需要stopSelf()

對於您報告的問題,您的整個循環將在onDestroy()被調用之前處理,因爲您現在已經執行了該循環。這是因爲在主應用程序線程上調用onStart()onDestroy(),對話框的onClick()方法也是如此。線程一次只能做一件事,並且只要您使用SMS發送循環綁定主應用程序線程,您將無法按下按鈕,並且您將無法停止該服務。

如果移動短信發送邏輯放在後臺線程,然後在onDestroy()你可以做一些事情來導致該線程終止(例如,紛紛跟帖看一場AtomicBoolean,你從onDestroy()翻轉)。

+0

所以我唯一的辦法是把它放在一個帶睡眠功能的線程?因爲當我試圖停止服務短信發送「任務」已經超出了服務的影響力? – whiteLT

2

你看不到一個麪包。 將一個在onDestroy()Log.e("myservice", "sadsad")而不是檢查,如果它被稱爲

+0

由於flooded logcat我看不到日誌顯示:/。當發送短信開始日誌貓正在充斥着各種各樣的信息 – whiteLT

+0

你可以在logcat窗口中設置一個過濾器 –

1

當你調用stopService(intent);它肯定不會打電話給你ServiceonDestroy()。問題可能是您的服務的onDestroy()中的Runnable/Thread無法運行。因此,在服務的onDestroy()內調用stopSelf();沒有任何意義,因爲onDestroy()只會在Service停止時調用。

1

如果您要綁定服務,則撥打stopService將不會停止該服務,除非您明確從Activity呼叫unbindService(connection);。從文檔 Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed

看到here

+0

我得到「服務未註冊」強制關閉。 – whiteLT

+0

請發佈完整的活動代碼。 – Sunny