【问题标题】:Android notify from my Alarm Class来自我的警报类的 Android 通知
【发布时间】:2014-02-06 13:06:55
【问题描述】:

我正在制作一个具有重复警报的应用程序,它检查来自 api 的日期,如果“日期”为真,它将发送通知。注释掉的通知是我正在使用的,因为它在我的 MainActivity 类中工作,我只是无法在我的 Alarm 类中得到通知。有什么想法吗?

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

【问题讨论】:

    标签: android notifications alarm


    【解决方案1】:

    检查这个:

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

    【讨论】:

    • 对不起,这是我的第一个应用程序,我是否将这些方法分开?我应该在 if 语句中添加哪一部分?
    • 在 if 语句中调用 showNotification(context);
    • 因为当我将第一部分放在 if 语句中时,我收到 IDE 错误“void is an invalid type for the variable onReceive”
    • 太好了,我的工作正常了,有点,它正在发送通知声音,但没有顶部通知标志
    • 刚刚开始工作。使用:setSmallIcon(R.drawable.ic_launcher)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多