如何在android中创建自定义通知

时间:2014-01-22 09:41:07

标签: java android notifications broadcastreceiver

我想在我的应用中创建自定义通知服务。 我不会使用谷歌服务(谷歌云消息)。 有没有办法创建一个守护进程,检查我的应用程序数据库上的每个X秒特定条件并显示通知? 谢谢,

修改

我在MainActivity的oncreate()中调用此方法。我从我的数据库中获取时间

    private void restartNotify() {
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pendingIntent);
    ArrayList<String> item = new ArrayList<String>();
    item = GetLists.GetTimesListForNotification(this);
    for (int i = 0; i < item.size(); ++i) {
        String time = item.get(i);
        int Position = time.indexOf(":");
        int hour = Integer.parseInt(time.substring(0, Position));
        int min = Integer.parseInt(time.substring(Position + 1));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        am.set(AlarmManager.RTC, cal.getTimeInMillis(),
                pendingIntent);
    }

这是我的广播接收器

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager mManager;
    mManager = (NotificationManager) context.getApplicationContext()
            .getSystemService(
                    context.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(context.getApplicationContext(),
            MainActivity.class);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "New message to read", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            context.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context.getApplicationContext(),
            "notification are work", "it's work",
            pendingNotificationIntent);

    mManager.notify(0, notification);
}

}

2 个答案:

答案 0 :(得分:1)

您可以像这样使用AlarmManager + BroadcastReceiver:

private void setRecurringAlarm(Context context) {
    Intent downloader = new Intent(this, MyStartServiceReceiver.class);
    downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, downloader, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 6000, 10000, pendingIntent);
}

BroadcastReceiver类:

public class MyStartServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    //do the stuff...

    }
}

不要忘记在清单中注册接收者:

<receiver android:name=".MyStartServiceReceiver"
        android:enabled="true"/>

答案 1 :(得分:0)

您必须使用Alarm Manager来安排每个特定时段执行的代码。看看这里: http://developer.android.com/training/scheduling/alarms.html