我必须实现一个在后台运行的服务,然后是一个不同的应用程序,它接受服务的结果并代表它。
我做了一项活动而不是启动服务,即使这个应用程序被销毁,服务也会继续运行。
活性:
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);
}
}
此代码运行正常。
但现在问题是..如何创建第二个访问此服务并获取坐标值的项目?
帮助?
由于