好的,我找到了一种方法:)也许它不是一个漂亮的方法,但它工作正常:
创建服务:
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
public class rtmpdumpService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
config = this;
String extras = "";
if(intent != null){
//Get needed information
extras = intent.getExtras().getString("rtmp");
}
else {
this.stopSelf();
}
doWork(extras);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void doWork(String rtmp){
//Do work here: for example rtmpdump
Rtmpdump dump = new Rtmpdump();
dump.parseString(params[0]);
System.exit(0);
this.stopSelf();
}
}
在 AndroidManifest 中注册为具有这些属性的服务
android:name=".rtmpdumpService"
安卓:导出=“假”
安卓:进程=“:rtmp”
启动服务:
Intent rtmpdumpIntent = new Intent(getApplicationContext(), rtmpdumpService.class);
eSendIntent.putExtra("rtmp", "RTMP CODE");
启动服务(rtmpdumpIntent);
有时您必须等到它完成:
服务启动后(startService(rtmpdumpIntent):
do {
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
//Log
}
} while( isServiceRunning(rtmpdumpService.class) == true);
isServiceRunning 函数:
private boolean isServiceRunning(Class cl) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (cl.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}