使用相同的服务沟通两个不同的项目

时间:2014-02-04 14:28:47

标签: java android service

我必须实现一个在后台运行的服务,然后是一个不同的应用程序,它接受服务的结果并代表它。

我做了一项活动而不是启动服务,即使这个应用程序被销毁,服务也会继续运行。

活性:

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;
        }

    }
}

服务:

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);
    }
}

此代码运行正常。

但现在问题是..如何创建第二个访问此服务并获取坐标值的项目?

帮助?

由于

1 个答案:

答案 0 :(得分:0)

如果您需要在服务中支持多线程,则需要AIDL。如果没有,则Mesenger将正常工作。 SDK附带的示例中有示例代码,您应该在api下的所有平台版本中找到它们,例如

C:\ YourRoot \工具\ Android的SDK-windows4.4 \样品\机器人-7 \ ApiDemos \ SRC \ COM \示例\机器人\的API \应用..

查找启动Remote ... java的文件和* .aidl

的文件