0

我收到错误上下文需要 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");
4

2 回答 2

1

好吧,这段代码被裁剪了,我看不到我需要的所有东西,但我可以看到你必须在调用 startActivity(i); 之前设置它;

    Intent i = new Intent(this, DataCountUtilities.class);         
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
于 2013-06-24T20:14:01.767 回答
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);  
于 2013-06-24T21:57:51.070 回答