问题描述
- 请问在服务中是否无法关闭服务呢???
-
public class MyService extends Service { private MyReceiver receiver; private class MyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { onDestroy(); } } public void onCreate() { super.onCreate(); Toast.makeText(MyService.this, "服务已启动", Toast.LENGTH_SHORT).show(); } public int onStartCommand(Intent intent, int flags, int startId) { receiver=new MyReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction("net.s5c.Changelink.wifiOff"); registerReceiver(receiver, filter); flags=START_STICKY; return super.onStartCommand(intent, flags, startId); } public void onDestroy() { Toast.makeText(this, "服务已关闭", Toast.LENGTH_LONG).show();//可以打印出这句,但看后台还是无法关闭服务! super.onDestroy(); } public IBinder onBind(Intent intent) { return null; } }
收到广播后能看到输出的onDestroy()的内容,但看正在运行中服务还是继续运行着,这是怎么回事呢?
解决方案
没有任何作用看了半天也没找到我需要的!
解决方案二:
http://www.tuicool.com/articles/iu22QnF
时间: 2024-11-08 23:26:54