2016-01-22 4 views
2

Я пытаюсь создать приложение, которое загружает (периодически) данные с сервера и уведомляет пользователя при загрузке.Отправка уведомления без кнопки Android

Я попытался использовать сообщение Toast, но мне нужно уведомление в строке состояния. Я использовал пример кода от developer.android.com, который уведомляет об этом после нажатия кнопки. Но мне нужно отправить уведомление, не нажимая кнопку.

Наиболее точный вопрос является

как вызвать sendNotification после «.Execute» в функции „запустить“, какие аргументы я должен использовать в sendNotification (Что здесь?)?

Этот h.postDelayed материал находится в OnCreate метод.

Вот код:

h.postDelayed(new Runnable() { 
     public void run() { 
      new WebServiceHandler() 
        .execute("http://maciekb94.cba.pl/"); 
        Toast.makeText(MainActivity.this, "Pobrano dane", Toast.LENGTH_SHORT).show(); 
        //sendNotification(); 
        h.postDelayed(this, delay); 
     } 
    },delay); 
} 

public void sendNotification(View view) { 

    Intent intent = new Intent(this,MainActivity.class); 
      //Uri.parse("http://developer.android.com/reference/android/app/Notification.html")); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 
    // END_INCLUDE(build_action) 
    // BEGIN_INCLUDE (build_notification) 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setSmallIcon(R.drawable.ic_stat_new_message); 

    // Set the intent that will fire when the user taps the notification. 
    builder.setContentIntent(pendingIntent); 

    // Set the notification to auto-cancel. This means that the notification will disappear 
    // after the user taps it, rather than remaining until it's explicitly dismissed. 
    builder.setAutoCancel(true); 
    builder.setContentTitle("Pobrano dane"); 
    builder.setContentText("Wejdź by je zobaczyć"); 


    NotificationManager notificationManager = (NotificationManager) getSystemService(
      NOTIFICATION_SERVICE); 
    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
    //END_INCLUDE(send_notification) 
} 
+0

один и тот же код, который вы реализовали –

+0

вызов 'метода sendNotification' везде, где вам нужно, чтобы показать уведомление. Пожалуйста, напишите больше кода для лучшей помощи. – Rohit5k2

+0

С какими аргументами я должен называть этот метод? – Maciekb

ответ

0

Отправьте свое уведомление в onPostExecute() своей задачи WebServiceHandler().

public class WebServiceHandler extends AsyncTask{ 

     @Override 
     protected Object doInBackground(Object[] params) { 

      .......... 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Object o) { 
      super.onPostExecute(o); 

      notificationManager.notify(NOTIFICATION_ID, builder.build()); 
     } 
    } 
+0

Но диспетчер уведомлений и построитель находятся в функции sendNotification, а в postExecute говорится: «Не удается разрешить символ» – Maciekb

+0

вызовите функцию sendNotification() в onPostExecute(). –

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