2013-09-16 103 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