android-How to run Service in different thread than main thread?

前端 未结 5 1857
旧时难觅i
旧时难觅i 2021-02-18 14:12

I am trying to develop a application in android that consists a service to read the sensor value for multiple hours. When i start the service my device get hang and all the othe

相关标签:
5条回答
  • 2021-02-18 14:34

    Application components (services, activities, etc) always run in main thread, no matter what thread they are started from. Consider starting thread in your Service instead, or use an IntentService.

    In your particular case you might try to register a global BroadcastReceiver for sensor changes, which, in turn,will start an IntentService to put newly acquired values in db, etc.

    Actually, here is the link to similar question solved.

    Again, this is not really a multithreading issue. The whole task must be implemented the other way.

    0 讨论(0)
  • 2021-02-18 14:46

    you can try using AsyncTask, please read this documentation.

    http://developer.android.com/reference/android/os/AsyncTask.html

    0 讨论(0)
  • 2021-02-18 14:47

    You can use Background Services to solve this problem. By using a Thread with sleep() for particular instance will give the solution to yours problem

    Background Servies

    This link Will help you..

    Or Using of PendingIntent will help you, like...

    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, ij, Intent.FLAG_ACTIVITY_NEW_TASK);
    
    0 讨论(0)
  • 2021-02-18 14:51

    I think the answer to this problem lies in the power of IntentService. IntentService is a subclass of the regular Service class. There are a few key differences, however.

    Now, here's how you can take IntentServices to your advantage: IntentServices do not run on the main thread. Instead, they run on separate "Worker threads". This single attribute would solve your problem and eradicate the ANRs you are currently facing.

    To learn more about the differences between services and IntentServices, check this out.

    0 讨论(0)
  • 2021-02-18 14:58

    All you are doing there is launching the new activity, so if your logic for running the monitor is in SensorService, then it's still going to be on the main thread. You need to put the monitoring logic into the new thread, not just launch the activity with it.

    If you are trying to run a service on a background thread you need to use the static performOnBackgrounThread method like this code which can be found in the Android documentation (android-8\SampleSyncAdapter\src\com\example\android\samplesync\client\NetworkUtilities.java):

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {
    
                }
            }
        };
        t.start();
        return t;
    }
    

    It is also important to remember that you never want to perform network operations on the Main UI thread. Not that you have here, just a FYI...

    0 讨论(0)
提交回复
热议问题