如何使用广播接收器停止服务

时间:2018-09-29 19:48:40

标签: java android service

我正在尝试使用stopService()停止服务,但该服务仍将运行。仅当我取消注册广播接收器时,它才会停止,但是当我再次注册它时,将同时激活两个操作。 该线程仍在以某种方式运行,我确实尝试了所有方法并阅读了论坛上的每条帖子,但没有找到解决方法。

Service Class

public class MyService extends Service {

    final static String MY_ACTION = "MY_ACTION";
    private static Context context;
    private Timer timer;
    public  String Data;
    String ok = "Database\n";

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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

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

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

        MyThread myThread = new MyThread();
        myThread.start();

        return super.onStartCommand(intent, flags, startId);
    }

    public class MyThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub

            try {
                int delay = 1000; // delay for 1 sec.
                int period = 5 * 1000; // repeat every 120 sec.
                timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        Calendar c = Calendar.getInstance();
                        Data = String.valueOf((c.get(Calendar.MILLISECOND)));
                        Intent intent = new Intent();
                        intent.setAction(MY_ACTION);
                        intent.putExtra("DATAPASSED", ok);
                        sendBroadcast(intent);
                    }
                }, delay, period);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            stopSelf();
        }

    }
}

MainActivity

    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MyService.MY_ACTION);
    registerReceiver(broadcastReceiver, intentFilter);
    final Intent intent = new Intent(MainActivity.this, MyService.class);
    //Start our own service

    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //registerReceiver(broadcastReceiver, intentFilter);
            startService(intent);
            }
    });

    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //unregisterReceiver(broadcastReceiver);
            stopService(intent);
        }
    });
}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    unregisterReceiver(broadcastReceiver);
    super.onStop();
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int datapassed = intent.getIntExtra("DATAPASSED", 0);
        String s = intent.getAction().toString();
        String s1 = intent.getStringExtra("DATAPASSED");
        textview.append(s1);
    }
};

1 个答案:

答案 0 :(得分:0)

我做错的是在我的线程中调用stopSelf(),这导致服务调用onDestroy()方法。我只是删除了它,并将myTimer和myTask添加为局部变量,然后可以在myDestroy上添加myTimer.cancel()并成功。