我有启动/停止服务的 Activity 。还有一个服务,它有一个循环。我目前正试图从主要 Activity 中停止我的服务。我尝试了很多变体,但没有调用 onDestroy :/。任何帮助、想法或教导将不胜感激。 :)
我尝试通过对话框停止服务的主要 Activity 的一部分:
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 :/,因为我没有看到 toast
更新:
我添加了一个线程,但不知何故它没有打印出来
@Override
public void run() {
try {
while(true) {
sleep(1000);
System.out.println("fff");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
最佳答案
首先,onStart()
已被弃用大约四年了。请使用 onStartCommand()
。
其次,请在后台线程中执行您的 SMS 工作,而不是像您目前所做的那样在主应用程序线程中执行。
第三,不要在onDestroy()
中调用stopSelf()
。如果您的服务到达 onDestroy()
,则不需要 stopSelf()
。
关于您报告的问题,您的整个循环将在调用 onDestroy()
之前得到处理,因为您现在已经实现了它。这是因为 onStart()
和 onDestroy()
都是在主应用程序线程上调用的,对话框的 onClick()
方法也是如此.一个线程一次只能做一件事,只要您将主应用程序线程与 SMS 发送循环 bundle 在一起,您就无法按下按钮,也无法停止服务。
如果您将 SMS 发送逻辑移动到后台线程中,那么在 onDestroy()
中您可以做一些事情来导致该线程终止(例如,让线程监视 AtomicBoolean
,你从 onDestroy()
中翻转。
关于android - 从 Activity 中停止服务循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18829258/