我收到错误上下文需要FLAG_ACTIVITY_NEW_TASK标志,尽管我已经设置了它。有什么建议吗?
public class DataCountService extends Service {
String text = "USR;1";
String ERROR = Constants.PREFS_NAME;
private Timer timer = new Timer();
private long period;
private long delay_interval;
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Constants.TAG, "Logging Service Started");
super.onStartCommand(intent, flags, startId);
Intent i = new Intent(this, DataCountUtilities.class);
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = intent.getExtras();
if (intent == null) {
// Exit gracefully is service not started by intent
Log.d(Constants.TAG, "Error: Null Intent");
答案 0 :(得分:1)
好吧,这段代码被裁剪,我看不到我需要的所有东西,但我能看到的是你必须在调用startActivity(i)之前设置它;
Intent i = new Intent(this, DataCountUtilities.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
答案 1 :(得分:1)
虽然@Opiatefuchs的答案很可能就是你想要的(你应该在>> 之前添加标志)你可能会面临另一个问题:
如果活动可以多次实例化(例如,如果服务多次启动),您还必须添加Intent.FLAG_ACTIVITY_MULTIPLE_TASK
标志:
Intent i = new Intent(this, DataCountUtilities.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);