-2
我是Android新手,我不知道这是怎么做的。 我需要在Android中执行一项服务,即使活动完成仍然存在。我这样做:创建一个服务,保持活着,甚至活动结束
活动:
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 OnClickListener {
private Button telo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
telo = (Button)findViewById(R.id.button1);
telo.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(this, PruebaService.class));
}
}
服务:
package com.example.pruebaservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class PruebaService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Servicio creado", Toast.LENGTH_LONG).show();
Log.d("SERVICEBOOT", "Servicio creado");
prueba();
}
private void prueba() {
// TODO Auto-generated method stub
for(;;){
Log.i("telo","motoor");
}
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Servicio destruido", Toast.LENGTH_LONG).show();
Log.d("SERVICEBOOT", "Servicio destruido");
}
}
但是,当活动是暂停或销毁服务崩溃。我怎样才能做到这一点?
感谢
问了这么多次... google for startForeground – Selvin