2014-02-06 2 views
0

Я делаю приложение с повторяющимся сигналом тревоги, которое проверяет дату с api, если «дата» истинна, он отправит уведомление. Отмеченное уведомление - это то, с чем я работаю, поскольку он работает в классе My MainActivity, я просто не могу получить его уведомление в моем классе Alarm. Есть идеи?Android уведомляет меня о своем классе тревоги

public class Alarm extends BroadcastReceiver 
    {  

     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
      wl.acquire(); 

      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      try { 
       request.setURI(new URI("exampleTimeUrl")); 
       HttpResponse response = client.execute(request); 

        HttpEntity entity = response.getEntity();    
        String responseText = EntityUtils.toString(entity); 
        JSONObject jsonObj = new JSONObject(responseText); 
        String istheday = jsonObj.getString("birthday"); 


        if (istheday=="true"){ 


         /// notify 
        /* 
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        Notification notify = new Notification(android.R.drawable.stat_notify_more, "Happy Birthday", System.currentTimeMillis()); 
        Context contextA = MainActivity.this; 
        CharSequence title = "happy birthday"; 
        CharSequence details = "yay"; 
        Intent intent2 = new Intent(contextA, MainActivity.class); 
        PendingIntent pending = PendingIntent.getActivity(contextA, 0, intent2, 0); 
        notify.setLatestEventInfo(contextA, title, details, pending); 
        nm.notify(0, notify); 
        */ 

         /// end of notify 





        } 


      } catch (URISyntaxException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      // end your code here 



      wl.release(); 

     } 

    public void SetAlarm(Context context) 
    { 
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 8000, pi); // Millisec * Second * Minute - will obviously increase this to run every day 
    } 

    public void CancelAlarm(Context context) 
    { 
     Intent intent = new Intent(context, Alarm.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 
} 

ответ

2

Проверьте это:

Notification notification; 
NotificationManager nm; 
NotificationManager mNotificationManager; 
NotificationCompat.Builder mBuilder; 
private static final int NOTIFICATION_ID = 1; 
    @Override 
    public void onReceive(Context context, Intent arg1) { 
     showNotification(context); 
    } 

    private void showNotification(Context context) { 
      Uri alarmSound = RingtoneManager 
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      if (alarmSound == null) { 
       alarmSound = RingtoneManager 
         .getDefaultUri(RingtoneManager.TYPE_RINGTONE); 
      } 

      mBuilder = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.image) 
        .setSound(alarmSound) 
        .setContentTitle("New Locations Found") 
        .setContentText(
          value 
            + " New Locations has been assigned to you"); 

      mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); 
      mBuilder.setAutoCancel(true); 

      Intent resultIntent = new Intent(this, HeliosActivity.class); 
      resultIntent.putExtra("skiphomescreen", true); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
      stackBuilder.addParentStack(HeliosActivity.class); 
      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder 
        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
      mBuilder.setContentIntent(resultPendingIntent); 
      mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

    } 
+0

извините, это мое первое приложение, я разделить эти методы? какую часть я вставляю в свое заявление if? – rolandnsharp

+0

вызовите 'showNotification (context);' в ваш оператор if – ryderz8

+0

, потому что я получаю ошибку IDE «void является недопустимым типом для переменной onReceive», когда я помещаю эту первую часть в stat if – rolandnsharp

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