How do I create a background loop method

by Laxs » Wed, 01 Sep 2010 07:19:55 GMT


Sponsored Links
 Hi,

I got a problem, I want to call a method every X minutes, if my method
got a result it should inform the user by vibrating or showing a
alertbox. if the method doesn't get a result it should wait for X
minutes and restart.
What is important, this should also work when the phone turns to
sleep. Which means I want to wake the phone from sleep to run my
method.

Actualy I can activate my method via a Button, and it is working like
I want it to be except the background loop thing.

I already searching for a solution here in the group or also on google
but I think I don't got the right search terms to find a solution for
my problme.

I already read about the AlarmManager and broadcast receiver but I'm
not sure if this is right for my problem.

I hope you could help me.

--



Other Threads

1. requestLocationUpdates and emulator

Hi to all, I am trying my applicaton with the emulator, and I am
experiencing the following problem: even if I registered a
locationlistener with minDistance = 0

mLManager.requestLocationUpdates(mLProvider, 10000, 0, mLListener);

the mLListener callback is called only when I set a new location via
the emulator.
Now my question is: what will happen with a real phone? The listener
will be called every 10 seconds as I expect, or only when the location
is changed?

Does the send location I do with the emulator simulate just one update
I would receive from the gps? I know I could get the answers just by
trying it on a real phone, but at the moment I don't have one yet :-)

Many thanks in advance,

Federico

-- 

2. Having trouble with Handler with Bluetooth

Hello, I am having such a frustrating time with this, I'm learning
about handlers and have hit a dead end.  I apologize if I'm in the
wrong forum

I have a bluetooth device that is sending the temperature to the
android phone in a constant stream.  I want to display the string on
the screen.  I have connected the bluetooth and I can read the
results, similar to bluetooth chat:

To summarize before I give you the code the message that I am suppose
to receive is 37.  So I have two problems:

1) sometimes I get 3, 7, or 37 in the debug (not on the screen).  I
can't figure out where it is getting out of sync

2) Handler isn't being called after a message is received, but after
the program crashes.  What am I doing wrong?

************Temperature Program*****

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

public void onStart() {
            super.onStart();
            dialog = ProgressDialog.show(this, "Connecting",
"Searching for a Bluetooth serial port...");
            thread = new CommThread(this, mHandler);
            thread.run();     //Runs CommThread Connection and I/
O
    }


//...Other code here...


    private final Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

                Log.d(TAG, "Calling Handler");
        }
};
}

*************************

*********************CommThread********************
try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        istream = tmpIn;
        ostream = tmpOut;

        StringBuffer sb = new StringBuffer();   //Used to read what
message says
        String message;                         //Used to read what message says
        byte[] buffer = new byte[1024];          // buffer store for the stream
        int bytes;                              // bytes returned from read()

        while (true) {
            try {
                // Read from the InputStream
                bytes = istream.read(buffer);

                //Determine what message says before handler
                sb.setLength(0);
                sb.append(new String(buffer, 0, bytes));
                message = sb.toString();
                Log.d(TAG, "The message is: " + message);

                //Call Handler
                mHandler.obtainMessage(Temperature.MESSAGE_READ,bytes,
0,buffer).sendToTarget();


            } catch (IOException e) {
                Log.e(TAG, "Exception during write!",e);
                break;
            }
        }
    }
*******************




***********Debug

04-23 13:42:04.552: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:04.552: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:05.552: DEBUG/BluetoothChatService(28755): The message is:
37
04-23 13:42:06.545: DEBUG/BluetoothChatService(28755): The message is:
37
04-23 13:42:07.547: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:07.571: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:08.546: DEBUG/BluetoothChatService(28755): The message is:
3
04-23 13:42:08.572: DEBUG/BluetoothChatService(28755): The message is:
7
04-23 13:42:28.949: ERROR/BluetoothChatService(28755): Exception
during write!
04-23 13:42:28.949: ERROR/BluetoothChatService(28755):
java.io.IOException: Software caused connection abort
..........
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler
04-23 13:42:28.989: DEBUG/BluetoothChatService(28755): Calling Handler

***********

So every time I receive a message I want to send it to the Handler,
like BluetoothChat program does.

The Handler isn't being called until the program crashes in the end.

Thanks for your help,

-- 

3. Progress bar is run after task completing

4. Cinemator 0.3

5. Android number formatting question

6. How to disable ListView scrolling?

7. Restlet librairies use in Android project