I've been trying to create an Alarm Manager to allow me to send an email to an hour. My application is a kind of calendar, so I will have many tasks and I need to differentiate programs. To do this I use the start time of the event as id.
This is my code:
public class EndEventTask extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String email = "someemaildirection@gmail.com";
Uri uri = Uri.parse(email);
Intent EmailActivity = new Intent(Intent.ACTION_SENDTO, uri);
//EmailActivity.putExtra(Intent.EXTRA_SUBJECT,
// "Customer comments/questions");
String message="Prueba de la aplicación del proyecto.";// message to send
EmailActivity.putExtra(Intent.EXTRA_TEXT, message);
EmailActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(EmailActivity);
}
}
And the Alarm Manager is sent from an activity that once the task is sent, activity will finish. As an example here ::
if(preferences.getBoolean("AlertCustomer", false)){
scheduleAlarmEnd(finalDate.getTimeInMillis());
}
Intent newIntent = getIntent();
newIntent.putExtra("title", dateTitleText);
newIntent.putExtra("beginDate", selectedDate.getTimeInMillis());
newIntent.putExtra("endDate", finalDate.getTimeInMillis());
newIntent.putExtra("siteLatBeg", latBeg);
newIntent.putExtra("siteLonBeg", lonBeg);
newIntent.putExtra("siteBegText", siteBegTextAutoComp);
newIntent.putExtra("siteLatDes", latDes);
newIntent.putExtra("siteLonDes", lonDes);
newIntent.putExtra("siteDesText", siteText);
newIntent.putExtra("mobile", mobileInt);
newIntent.putExtra("email", emailText);
newIntent.putExtra("duration", duration + extraTime);
this.setResult(RESULT_OK, newIntent);
finish();
...............
private void scheduleAlarmEnd(long time)
{
Intent intentAlarm = new Intent(this, EndEventTask.class);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pIntent = PendingIntent.getBroadcast(this,
(int) time, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, pIntent);
}
And this is my Mainfest:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
.......................
<receiver android:name="com.android.task.BegEventTask">
<intent-filter>
<action android:name="android.intent.action.ACTION_SENDTO" />
</intent-filter>
</receiver>
And finally I get an error when starting the intent (context.startActivity(EmailActivity);):
07-17 11:37:41.059: E/AndroidRuntime(2647): FATAL EXCEPTION: main
07-17 11:37:41.059: E/AndroidRuntime(2647): java.lang.RuntimeException: Unable to start receiver com.android.task.EndEventTask: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=someemaildirection@gmail.com flg=0x10000000 }
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2287)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ActivityThread.access$1600(ActivityThread.java:140)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.os.Looper.loop(Looper.java:137)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-17 11:37:41.059: E/AndroidRuntime(2647): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:37:41.059: E/AndroidRuntime(2647): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:37:41.059: E/AndroidRuntime(2647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-17 11:37:41.059: E/AndroidRuntime(2647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-17 11:37:41.059: E/AndroidRuntime(2647): at dalvik.system.NativeStart.main(Native Method)
07-17 11:37:41.059: E/AndroidRuntime(2647): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=someemaildirection@gmail.com flg=0x10000000 }
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1580)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ContextImpl.startActivity(ContextImpl.java:1054)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ContextImpl.startActivity(ContextImpl.java:1043)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.content.ContextWrapper.startActivity(ContextWrapper.java:283)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.content.ContextWrapper.startActivity(ContextWrapper.java:283)
07-17 11:37:41.059: E/AndroidRuntime(2647): at com.android.task.EndEventTask.onReceive(EndEventTask.java:31)
07-17 11:37:41.059: E/AndroidRuntime(2647): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)
07-17 11:37:41.059: E/AndroidRuntime(2647): ... 10 more