1

I am new to android and i was doing some application which might use multi threading. For example the application threads might do as follows assuming 2 threads;

Thread 1 Even if the overall application runs on foreground thread one should run at all times listening for specific sms; Imagine the sms to be Intercepted is "3456" when this message is sent to the phone then thread one will be paused and thread 2 will be activated:

Thread 2 When thread to is activated then it will use gps to track the location of the phone and will use instance of smsManager to send back the coordinates(log, lat) of the phone or even if possible google map back to the phone which sent the message "3456" and then thread one will be activated:

**How to make this happen any idea?

Mudasar
  • 249
  • 1
  • 5
  • 15

2 Answers2

8

There are two answers to this question.

  1. If you want to run a thread in the background over a long period of time, to listen for events or run a regular process then Services are the way to go

  2. If you need to fire off a new thread to do some processing once and then stop, then look at AsyncTask which is a very, very easy way to do that, and includes a simple way to update the user interface during the process.

The developer docs contain an excellent page about location in Android

Here's some information about receiving SMS in your app

Community
  • 1
  • 1
Ollie C
  • 28,313
  • 34
  • 134
  • 217
  • 1
    I've never used AsyncTask. Is that for fire-and-forget threading? I usually use either Services (as you've suggested) or Handler/Runnable if I need something inline from an Activity, for example. Know of any articles outlining the differences between AsyncTask and Handler/Runnable? – Rich Apr 01 '11 at 18:26
  • iam using a service in which i add a broadcast receiver but the problem is that i cant find the registration commands of that broadcast receiver. – Mudasar Apr 01 '11 at 18:27
  • I prefer AsyncTask as I find it very clean and easy, but I think it's just a matter of preference which you use. – Ollie C Apr 01 '11 at 18:32
  • yeah i had sms code which receive the sms but problem is that when i call a activity after receiving the sms the activity is not called because this is broadcast receiver not a activity as we know the activity can another activity thats why i want to put broadcast receiver in a service but unable to find the permissions for broadcast receiver in a service :( – Mudasar Apr 01 '11 at 18:33
  • I've answered your question, but you're saying the problem is something else? I think you need to ask the question more clearly, in a new question post, so people can help you. – Ollie C Apr 01 '11 at 18:39
2

Have a look at Services. A lot of this does not have to be explicitly coded if you make use of Services in your application.

http://developer.android.com/reference/android/app/Service.html

Edit

In response to comments, here's how I do communication from Service to Activity using BroadcastReceiver

public class SomeActivity extends Activity {

BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Example of pulling a string out of the Intent's "extras"
        String msg = intent.getStringExtra("BroadcastString");

        // ...more stuff

    }
};


@Override
public void onResume()
{
    super.onResume();
    registerReceiver(receiver, new IntentFilter("SomeStringKeyForMyBroadcast"));

    // ... other stuff
}

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(receiver);

    // ... other stuff
}

and in my Service...

public class SomeService extends Service {

Intent broadcastIntent = new Intent("SomeStringKeyForMyBroadcast");

private void someWorkerMethodInMyService()
{
        // ... other stuff

        broadcastIntent.putExtra("BroadcastString", "Some Data");
        sendBroadcast(broadcastIntent);

        // ... other stuff
}

something like that...

Rich
  • 36,270
  • 31
  • 115
  • 154
  • iam using a service in which i add a broadcast receiver but the problem is that i cant find the registration commands of that broadcast receiver. – Mudasar Apr 01 '11 at 18:21
  • I delcare my BroadcastReceiver in my Activity, registerReceiver in onResume and unregisterReciever in onPause. In my service I pass an Intent to sendBroadcast. The Intent is instantiated with the same string key as the BroadcastReceiver is registered with – Rich Apr 01 '11 at 18:29
  • thanks man . so in this code when are you able to call another activity after calling the broadcast receiver? because my application unable to call activity after calling broadcast receiver :( – Mudasar Apr 01 '11 at 18:47
  • what do you mean "call Activity"? – Rich Apr 01 '11 at 19:01
  • means i have two clases one is main class which is extends to activity and other is extends to broadcast receiver which is listing for sms when sms come it listen and parse it but when i want to pass this parsed data to first class which is extended with activity it dsnt pass it so "how to call an activity from BroadCastReceiver?" – Mudasar Apr 01 '11 at 19:08
  • You don't call Activity from BroadcastReciever...you define the BroadcastReceiver in scope of an Activity and thus the BroadcastReceiver inherits a scope that allows it to talk directly to the Activity. Check my example above...BroadcastReceiver is a member variable of the Activity which allows the Activity to register it in the first place – Rich Apr 01 '11 at 19:10
  • Btw...that is a working example pulled directly out of one of my apps, so it works the way it is. I just obscured it a little bit by changing strings and class names. – Rich Apr 01 '11 at 19:11
  • can u please send me full code so i can understand it at this moment i cant understand it :( – Mudasar Apr 01 '11 at 19:38
  • There is a ton of code there...enough to get a BroadcastReciever wired up from both sender and reciever. If it doesn't make sense, read the documentation further to better understand the mechanisms, terminology, etc. – Rich Apr 01 '11 at 20:04
  • i read lot of material but still not successed to make pleases send me just send me full code of above two classes iam thankful to u – Mudasar Apr 01 '11 at 20:07
  • I'm not sending you my entire application. lol. I just showed you all the code you need to make it work. – Rich Apr 01 '11 at 20:13