2011-09-21 54 views
4

我在日曆中插入了一個事件。創建事件的時間也必須在同一時間響起的警報創建。怎麼做?我使用了下面的代碼,它給出了錯誤。 主要活動:如何在創建日曆事件時創建鬧鐘?

Calendar caln = Calendar.getInstance(); 

    caln.add(Calendar.SECOND, 2); 
    Intent intent = new Intent(ToDoApplicationActivity.this, AlarmReceiver.class); 
    intent.putExtra("alarm_message", title1); 

    PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
    startActivity(intent); 

的onReceive方法overrided:

public void onReceive(Context context, Intent intent) { 
    try { 
    Bundle bundle = intent.getExtras(); 
    String message = bundle.getString("alarm_message"); 

    Intent newIntent = new Intent(context, ToDoApplicationActivity.class); 
    newIntent.putExtra("alarm_message", message); 
    newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(newIntent); 
    } catch (Exception e) { 
    Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
    e.printStackTrace(); 

    } 
} 

錯誤了:

android.content

當我使用下面的代碼我得到以下錯誤。 ActivityNotFoundException:無法找到顯式活動類{com.android.todoapplication/android.todoapplica tion.AlarmReceiver};你有沒有在你的AndroidManifest.xml中聲明這個活動?

任何幫助,高度讚賞,並在此先感謝...

回答

2

嘗試添加您的活動「AlarmReceiver」AndroidManifest.xml
如果你的活動延伸一個Android接收機(延續:廣播接收器),添加這樣的:

<application android:label="@string/app_name" ...> 
    ... 

    <receiver android:name=".AlarmReceiver" android:process=":remote" /> 
</application> 

其他

<application android:label="@string/app_name" ...> 
    ... 

    <activity android:name=".AlarmReceiver"/> 
</application>