Android: calling a method from a Service to an Activity

I have a simple question to solve, but I am not sure how to do it.

I have a class that extends Service that runs a thread looking for a TCP connection. If one comes in, it read an input message.

If the input message is "START", I start an activity, in this fashion:

Intent dialogIntent = new Intent(getBaseContext(), VoIPCall.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("inetAddress", clientSocket.getInetAddress());
getApplication().startActivity(dialogIntent);

While this activity is running, the Service keeps running. At some point I may reserve a "STOP". I would like to call a method in the previously created Activity but I am not sure how to interact with it.

I do not want to use a static method. How can I please do that?

Thank you very much,

EDIT: I changed my code to this:

Intent dialogIntent = new Intent("com.voip.rudy.START_CALL");
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
dialogIntent.putExtra("inetAddress", clientSocket.getInetAddress());
getApplication().startActivity(dialogIntent);

And in the manifest:

<activity android:name=".VoIPCall">
    <intent-filter>
        <action android:name="com.voip.rudy.START_CALL" />
        <action android:name="com.voip.rudy.STOP_CALL" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>


<category android:name="android.intent.category.DEFAULT" /> was required to avoid having it crash.

EDIT:

The solution given has fixed my issue but I wanted to actually act on member variables on that class, that are previously initialized. Say I call the constructor, then I would like to go back into this activity and act on the member variables.

The member variables are not initialized when I call one action after another, it seems to create a new activity somehow. Would there be anyway to act on the same activity, and keep the objects intact please?

James

Answers


Add the Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flags to your intent and call startActivity with a new action or a special extra that can identify the intent.

In the activity, write a method called :

private void handleIntent(Intent intent) {
}

call this from onCreate (or onResume) using:

handleIntent(getIntent());

and also write:

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

Need Your Help

how to fit pdf page in entire view

iphone ipad pdf sqlite3 nsxmlparser

i read pdf files and also displayed, the code is like this:

Why a uitextfield inside a uiscroll view doesn't scroll?

iphone ios uiscrollview uitextfield uitextview

I have a scrollview which contains a text field and a text view. I would that both two scrolled together. How can I do? Now only the text view scroll.

IE8 in windows xp opens .pdf, but not in windows 7

asp.net-mvc pdf windows-7 internet-explorer-8 cross-browser

I have a site that lists customer letters, and when a letter's "view" button is clicked it uses JavaScript (window.open(url)) to open a new window to get the .pdf file, which I think is generated by

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.