2014-01-22 3 views
1

Я хотел бы создать службу пользовательских уведомлений в своем приложении. Я бы не использовал службу google (Google Cloud Messaging). Есть ли способ создать демона, который проверяет каждые X секунд определенное условие на моем приложении db и показывает уведомление? Спасибо,Как создать пользовательское уведомление в android

редактировать

Я называю этот метод OnCreate MainActivity (в). Я получаю раз от моего дб

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); 
} 

}

+1

Почему вы не показываете уведомление от GCMReciever? –

+0

Благодарим за сообщение. Я не хочу использовать сервер – Anto

+1

Вы можете использовать этот класс http://developer.android.com/reference/android/app/Service.html –

ответ

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"/> 
+0

Спасибо. Я рассказал вам ваши советы. Но знайте, когда приходит уведомление, и я «нажимаю» на нее, после открытия действия, присылаю другое уведомление. Как петля. Я редактирую свой ответ, чтобы показать вам. Дайте мне 2 минуты, чтобы отредактировать его. Tks P.S. Извините за мой английский – Anto

Смежные вопросы