Android Open Source - android-for-dummies-v3 On Alarm Receiver






From Project

Back to project page android-for-dummies-v3.

License

The source code is released under:

Apache License

If you think the Android project android-for-dummies-v3 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.dummies.tasks.receiver;
/*from  w ww . ja va 2s. c o m*/
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.dummies.tasks.R;
import com.dummies.tasks.activity.TaskEditActivity;
import com.dummies.tasks.provider.TaskProvider;

/**
 * This class is called when our reminder alarm fires,
 * at which point we'll create a notification and show it to the user.
 */
public class OnAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Note: Do not do any asynchronous operations in
        // BroadcastReceive.onReceive!
        // The OS may kill your process immediately after onReceive
        // returns, so if you attempt to do asynchronous operations
        // in onReceive, they may get killed before they ever finish!
        // The result will be that sometimes things will appear to work,
        // and sometimes they won't.
        // If you need to do asynchronous
        // operations (eg. network requests, disk or database reads or
        // writes, etc.), then update OnAlarmReceiver to subclass
        // android.support.v4.content.WakefulBroadcastReceiver and
        // create a new service to do all of your heavy lifting.
        // Remember to call startWakefulService to start your service,
        // and remember to call
        // WakefulBroadcastReceiver.completeWakefulIntent from your
        // service when you are done.

        NotificationManager mgr = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        // Create the intent that will open the TaskEditActivity
        // for the specified task id.  We get the id of the task
        // from the OnAlarmReceiver's broadcast intent.
        Intent taskEditIntent =
                new Intent(context, TaskEditActivity.class);
        long taskId = intent.getLongExtra(TaskProvider.COLUMN_TASKID, -1);
        String title = intent.getStringExtra(TaskProvider.COLUMN_TITLE);
        taskEditIntent.putExtra(TaskProvider.COLUMN_TASKID, taskId);

        // Create the PendingIntent that will wrap the
        // taskEditIntent.  All intents that are used in
        // notifications must be wrapped in a PendingIntent to "give
        // permission" to the OS to call back into our
        // application when the notification is clicked.
        PendingIntent pi = PendingIntent.getActivity(context, 0,
                taskEditIntent, PendingIntent.FLAG_ONE_SHOT);

        // Build the Notification object using a Notification.Builder
        Notification note = new Notification.Builder(context)
                .setContentTitle(context.getString(R.string
                        .notify_new_task_title))
                .setContentText(title)
                .setSmallIcon(android.R.drawable.stat_sys_warning)
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();

        // Send the notification.
        mgr.notify((int) taskId, note);
    }
}




Java Source Code List

com.dummies.helloandroid.ApplicationTest.java
com.dummies.helloandroid.MainActivity.java
com.dummies.silentmodetoggle.MainActivity.java
com.dummies.silentmodetoggle.util.RingerHelper.java
com.dummies.silentmodetoggle.widget.AppWidgetService.java
com.dummies.silentmodetoggle.widget.AppWidget.java
com.dummies.tasks.activity.PreferencesActivity.java
com.dummies.tasks.activity.TaskEditActivity.java
com.dummies.tasks.activity.TaskListActivity.java
com.dummies.tasks.adapter.TaskListAdapter.java
com.dummies.tasks.fragment.DatePickerDialogFragment.java
com.dummies.tasks.fragment.PreferencesFragment.java
com.dummies.tasks.fragment.TaskEditFragment.java
com.dummies.tasks.fragment.TaskListFragment.java
com.dummies.tasks.fragment.TimePickerDialogFragment.java
com.dummies.tasks.interfaces.OnEditFinished.java
com.dummies.tasks.interfaces.OnEditTask.java
com.dummies.tasks.provider.TaskProvider.java
com.dummies.tasks.receiver.OnAlarmReceiver.java
com.dummies.tasks.receiver.OnBootReceiver.java
com.dummies.tasks.tablet.activity.TaskListAndEditorActivity.java
com.dummies.tasks.util.ReminderManager.java