警报不起作用

时间:2012-02-14 06:36:00

标签: android

我写下面的代码。 我在清单中宣布BroadcastReciever仍然无法正常工作。 我是一个乞丐,并坚持了2个小时。 这可能有什么问题?

// My launcher activity
public class AlarmMainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        GregorianCalendar gc=new GregorianCalendar();
        gc.set(2012, 1, 14, 11, 28);

        Intent intent=new Intent(this, AlarmService.class);
        gc.set(Calendar.AM_PM, 0);

        PendingIntent sender=PendingIntent.getBroadcast(this, 1, intent, 0);
        am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), sender);
        System.out.print(gc);
    }
}

// broadcast reciever class

public class AlarmService extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm started", 2000).show();
    }
}

1 个答案:

答案 0 :(得分:0)

NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context.getSystemService(
        Context.NOTIFICATION_SERVICE);
    CharSequence from = "Check your ....";
    CharSequence message = "It's time!";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
    Notification notif = new Notification(R.drawable.ic_launcher,
        "ur text", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.defaults |= Notification.DEFAULT_SOUND; 
    notif.flags |= Notification.FLAG_AUTO_CANCEL; 
    nm.notify(1, notif);