1

I have to implement a service that runs in background and then an different app that take service's results and represent its.

I did an activity than launch a service and even if this app is destroyed service continues to run.

activity:

package com.example.pruebaservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button telo, telo2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        telo = (Button) findViewById(R.id.button1);
        telo.setOnClickListener(this);
        telo2 = (Button) findViewById(R.id.button2);
        telo2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.button1:
            startService(new Intent(this, PruebaService.class));
            break;
        case R.id.button2:
            stopService(new Intent(this, PruebaService.class));
            break;
        }

    }
}

service:

package com.example.pruebaservice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class PruebaService extends Service {

    private Sistole_main telo;
    private static final String SISTOLE_FOLDER = "SISTOLE_Audio";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        Toast.makeText(this, "Servicio creado", Toast.LENGTH_LONG).show();
        Log.d("SERVICEBOOT", "Servicio creado");

        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath, SISTOLE_FOLDER);
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(file.getAbsolutePath() + "/" + "trig.pcm");
        if (!file2.exists()) {
            AssetManager manager = getApplicationContext().getAssets();
            try {
                InputStream movefile = manager.open("trig.pcm");
                String ruta = file.getAbsolutePath() + "/" + "trig.pcm";
                OutputStream salida = new FileOutputStream(ruta);

                byte[] buffer = new byte[1024];
                int tam;
                while ((tam = movefile.read(buffer)) > 0) {
                    salida.write(buffer, 0, tam);

                }
                salida.flush();
                salida.close();
                movefile.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


        @SuppressWarnings("deprecation")
        Notification note = new Notification(R.drawable.gradiant,
                "Servicio SISTOLE", System.currentTimeMillis());

        Intent i = new Intent(this, MainActivity.class);

        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

        note.setLatestEventInfo(this, "SISTOLE",
                "Calculando coordenadas", pi);
        note.flags |= Notification.FLAG_NO_CLEAR;

        startForeground(1337, note);

        telo = new Sistole_main();
        telo.control(1);

    }

    @Override
    public void onDestroy() {

        Toast.makeText(this, "Servicio destruido", Toast.LENGTH_LONG).show();
        Log.d("SERVICEBOOT", "Servicio destruido");
        telo.control(0);
    }
}

this code runs OK.

But now the question.. How can I do to create a second project that access to this service and take coordinates values?

Help?

Thanks

user3243651
  • 241
  • 2
  • 4
  • 14
  • I think you have to use a `ContentProvider` to provide access between different apps. – Rarw Feb 04 '14 at 14:30

1 Answers1

0

If you need to support multithreading in your service then you need AIDL. If not then a Mesenger will work OK. There is example code in the samples that come with the SDK, you should find them in all platform revisions under api e.g

C:\YourRoot\tools\android-sdk-windows4.4\samples\android-7\ApiDemos\src\com\example\android\apis\app..

Look for the files starting Remote...java and those which are *.aidl

NickT
  • 23,844
  • 11
  • 78
  • 121
  • Check [here](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) for a wonderful example – upenpat Mar 25 '14 at 09:46
  • @upenpat - not that wonderful really, as both the activity and the service are in the same application. If you had looked at the manifest more carefully, you would have noticed that. – NickT Mar 25 '14 at 09:51
  • what i meant was that it was simple example which demonstrates Messenger usage. – upenpat Mar 25 '14 at 09:54