使用AlarmManager计划任务

时间:2016-01-09 10:51:50

标签: android alarmmanager

我正在尝试安排任务"敬酒消息"出现在用户选择的时间内,但在此之后没有显示任何内容,我无法查看代码中的错误。 这是我的代码

public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        //set The Calendar to to the wanted time!
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);

        Intent setMsg = new Intent(MainActivity.this, TaskReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, setMsg, 0);
        AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        manager.setExact(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis(), pendingIntent);

    }
}


public class TaskReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "it is working!", Toast.LENGTH_SHORT).show();
    }
}

如果有更好的方法来安排这样的任务,请继续介绍它。

2 个答案:

答案 0 :(得分:0)

在您的代码中使用它 而不是manager.setExact methode

manager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),interval,pendingIntent);

答案 1 :(得分:0)

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
// Global variables

string name;
int nic;
int contact;
string address;
int customerId;
string status;
string todaysVehicle;
string vnum;
string serviceType;

int GetInput()
{

    int choice;
    cin >> choice;
    return choice;
}


struct list
{
    string name;
    int nic;
    int contact;
    string address;
    int customerId;
    string status;
    string todaysVehicle;
    string vnum;
    string serviceType;
    list *next;
};

// List Function Prototypes

bool empty(list *head);
void DisplayMenu();
void firsttElement(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void insertCusomter(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void removeCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void customerList(list *current);


bool empty(list *head){
    if (head == NULL)
        return true;

    else
        return false;
}


// Main menu of the program

void DisplayMenu(){

}

void firstElement(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
    list *temp1 = new list;
    temp1->nic = nic;
    temp1->next = NULL;
    head = temp1;
    last = temp1;

    list *temp2 = new list;
    temp2->name = name;
    temp2->next = NULL;
    head = temp2;
    last = temp2;

    list *temp3 = new list;
    temp3->nic = contact;
    temp3->next = NULL;
    head = temp3;
    last = temp3;

    list *temp4 = new list;
    temp4->address = address;
    temp4->next = NULL;
    head = temp4;
    last = temp4;

    list *temp5 = new list;
    temp5->nic = customerId;
    temp5->next = NULL;
    head = temp5;
    last = temp5;

    list *temp6 = new list;
    temp6->todaysVehicle = todaysVehicle;
    temp6->next = NULL;
    head = temp6;
    last = temp6;

    list *temp7 = new list;
    temp7->vnum = vnum;
    temp7->next = NULL;
    head = temp7;
    last = temp7;

}

void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->nic = nic;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->name = name;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->address = address;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->nic = contact;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->nic = customerId;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->todaysVehicle = todaysVehicle;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }

    if (empty(head))
        firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);

    else {

        list *temp = new list;
        temp->vnum = vnum;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
}

void removeCustomer(list *&head, list *&last){
    if (empty(head))
        cout << "There are no Customers in the Database! \n";

    else if (head == last){
        delete head;
        head = NULL;
        last = NULL;
    }
    else{
        list *temp = head;
        head = head->next;
        delete temp;
    }
}

void customerList(list *current){
    if (empty(current))
        cout << "The list is empty \n;";
    else
    {
        cout << "The list contains: \n";
        while (current != NULL){
            cout << current->customerId << endl;
            cout << current->nic << endl;
            cout << current->name << endl;
            cout << current->vnum << endl;
            cout << current->todaysVehicle << endl;
            cout << current->vnum << endl;
            cout << current->contact << endl;
            current = current->next;
        }
    }


}

// Main Fucntion

int main(int argc, _TCHAR* argv[], void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum), void removeCustomer(list *&head, list *&last), void customerList(list *current)){

    list *head = NULL;
    list *last = NULL;
    int choice;

    do{
        system("cls");
        cout << "Clean Park Daily Car Services\n";
        cout << "******************************\n";
        cout << "Please make your selection\n";
        cout << " \n";
        cout << "1 - Register Customer\n";
        cout << "2 - Request for Serivce\n";
        cout << "3 - UnRegister Customer\n";
        cout << "4 - Update Customer\n";
        cout << "5 - View Customer\n";
        cout << "6 - View all Customers\n";
        cout << "7 - Exit\n";
        cout << "\n";
        cout << "Choice: ";

        cin >> choice;

        DisplayMenu();
        switch (choice){

        case 1: // Register a new customer
            system("CLS");
                cout << "PLease enter Customer National Id: \n";
                cin >> nic;
                cout << " Enter Customer Name: \n";
                cin >> name;
                cout << "Enter Customer Contact Number: \n";
                cin >> contact;
                cout << "Enter Customer Address: \n";
                cin >> address;
                cout << "Enter Customer Number: \n";
                cin >> customerId;
                cout << "Enter Vehicle's Date: \n";
                cin >> todaysVehicle;
                cout << "Enter Vehicle Number: \n";
                cin >> vnum;
                void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
            break;

        case 2: // Request for Service
            system("CLS");

        case 3: // Remove customer
            system("CLS");
            removeCustomer(*&head, *&last);
            break;

        case 4: // Update customer
            system("CLS");

        case 5: // View Customer
            system("CLS");

        case 6: // View All Customers
            system("CLS");
            void customerList(list *current);
            break;

        case 7: // Exit Application
            cout << "Goodbye!.." << endl;
            exit(0);

        default:
            cout << "System exit \n";
        }
    } while (choice != 7);
    return 0;
}