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

make a panel form in the center of the page

asp.net css vb.net twitter-bootstrap-3

so I was trying to create a form panel with Twitter Bootstrap into the center of the page, and hopefully even when the browser is resizing, it still be at the center,

Issue with PDO fetch

php pdo

I have this snippet of code that completely screws up when I run it. It doesn't even throw an exception. It just causes a server error.

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.