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 没有以某种方式被调用:/,因为我没有看到吐司

更新:

我添加了一个线程,但不知何故它没有打印出来

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

                thread.start();
4

4 回答 4

2

首先,onStart()已经弃用了大约四年。请使用onStartCommand().

其次,请在后台线程中进行 SMS 工作,而不是像您目前正在做的主应用程序线程。

第三,不要打电话stopSelf()onDestroy()如果你的服务达onDestroy()不到,你就不需要了stopSelf()

关于您报告的问题,您的整个循环将在onDestroy()调用之前处理,因为您现在已经实现了它。这是因为onStart()onDestroy()都在主应用程序线程上调用,onClick()对话框的方法也是如此。一个线程一次只能做一件事,只要您将主应用程序线程与您的 SMS 发送循环捆绑在一起,您将无法按下按钮并且您将无法停止服务。

如果您将 SMS 发送逻辑移动到后台线程中,那么onDestroy()您可以做一些事情来使该线程终止(例如,让线程监视AtomicBoolean您从 翻转的onDestroy())。

于 2013-09-16T13:41:30.103 回答
2

您没有看到 toast,因为您正在使用getApplicationContext(). 放一个Log.e("myservice", "sadsad")代替onDestroy()以检查它是否被调用

于 2013-09-16T13:34:01.803 回答
1

当您调用stopService(intent);它时,它肯定会调用onDestroy()您的Service. 问题可能是您不是onDestroy()您的服务的可运行/线程。所以在 ServicestopSelf();内部调用onDestroy()没有任何意义,因为onDestroy()只有在 Service 停止时才会调用。

于 2013-09-16T13:35:13.530 回答
1

如果您正在绑定服务,那么调用stopService将不会停止服务,直到您unbindService(connection);Activity. 从文档 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

这里

于 2013-09-16T13:44:02.787 回答