通常我用这段代码开始一个活动:
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
但是如何开始活动以便留在后台呢?
要使活动在后台运行,可以使用服务.创建一个后台服务,如:
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BackgroundService extends Service {
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
BackgroundService getService() {
return BackgroundService.this;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
在主活动的oncreate()中调用服务,如下所示 -
startService(new Intent( MainActivity.this,BackgroundService.class));
标签:class,BackgroundService,public,Intent,Activity,后台,import,Android,android From: https://www.cnblogs.com/yelanggu/p/17047469.html