一个接一个地开始两个意图

时间:2014-07-27 12:07:14

标签: android android-intent calendar

我正在尝试启动两个日历意图,然后在单击按钮时启动另一个,但它不起作用。

代码:

        Intent calIntent1 = new Intent(Intent.ACTION_INSERT);
        calIntent1.setType("vnd.android.cursor.item/event");   
        calIntent1.putExtra(Events.TITLE, "TITLE");

        GregorianCalendar calDate1 = new GregorianCalendar(year, month, day, hour, min);
        calIntent1.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
        calIntent1.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
        calDate1.getTimeInMillis());
        calIntent1.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
        calDate1.getTimeInMillis()+2*60*60*1000);

        Intent calIntent2 = new Intent(Intent.ACTION_INSERT);
        calIntent2.setType("vnd.android.cursor.item/event");   
        calIntent2.putExtra(Events.TITLE, "TITLE2");

        GregorianCalendar calDate2 = new GregorianCalendar(year, month, day, hour, min);
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
        calDate2.getTimeInMillis());
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
        calDate2.getTimeInMillis()+2*60*60*1000);

        startActivity(calIntent2); startActivity(calIntent1);

知道为什么不工作?

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用 onActivityResult方法。你将开始1个意图并等待它的结果。然后你可以开始你的第二个意图。一些示例代码:

package **yourpackagename**;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;

public class TwoIntentClass extends Activity {

public static final int REQUEST_FIRST_INTENT = 10;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // call your method here
    yourMethodName(2014, 9, 10, 5, 20);

}

public void yourMethodName(int year, int month, int day, int hour, int min) {
    Intent calIntent1 = new Intent(Intent.ACTION_INSERT);
    calIntent1.setType("vnd.android.cursor.item/event");
    calIntent1.putExtra(Events.TITLE, "TITLE");

    GregorianCalendar calDate1 = new GregorianCalendar(year, month, day, hour, min);
    calIntent1.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
    calIntent1.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate1.getTimeInMillis());
    calIntent1.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate1.getTimeInMillis() + 2 * 60 * 60 * 1000);

    startActivityForResult(calIntent1, REQUEST_FIRST_INTENT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // if this is true, this means your first intent finished. So you can start your second intent
    if (requestCode == REQUEST_FIRST_INTENT) {

        Intent calIntent2 = new Intent(Intent.ACTION_INSERT);
        calIntent2.setType("vnd.android.cursor.item/event");
        calIntent2.putExtra(Events.TITLE, "TITLE2");

        GregorianCalendar calDate2 = new GregorianCalendar(year, month, day, hour, min);
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false);
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate2.getTimeInMillis());
        calIntent2.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate2.getTimeInMillis() + 2 * 60 * 60 * 1000);

        startActivity(calIntent2);
    }
}
}