Android的从我的报警类别通知 [英] Android notify from my Alarm Class

查看:196
本文介绍了Android的从我的报警类别通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做,有一个重复报警,从一个API检查日期的应用程序,如果约会是真实的,它会发送通知。注释掉通知就是我的工作,因为它在我的MainActivity类别,我只是不能让它在我的报警类别通知。任何想法?

 公共类报警扩展广播接收器
    {         @覆盖
         公共无效的onReceive(上下文的背景下,意图意图)
         {
             电源管理PM =(电源管理)context.getSystemService(Context.POWER_SERVICE);
             PowerManager.WakeLock WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,);
             wl.acquire();             HttpClient的客户端=新DefaultHttpClient();
             HTTPGET请求=新HTTPGET();
             尝试{
                request.setURI(新的URI(exampleTimeUrl));
                HTT presponse响应= client.execute(请求);                    HttpEntity实体= response.getEntity();
                    字符串的responseText = EntityUtils.toString(实体);
                    JSONObject的jsonObj =新的JSONObject(responseText的);
                    字符串istheday = jsonObj.getString(生日);
                    如果(istheday ==真){
                        ///通知
                    / *
                    NotificationManager纳米=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                    通知通知=新的通知(android.R.drawable.stat_notify_more,生日快乐,System.currentTimeMillis的());
                    上下文contextA = MainActivity.this;
                    CharSequence的标题=生日快乐;
                    CharSequence的细节=耶;
                    意图int​​ent2 =新意图(contextA,MainActivity.class);
                    的PendingIntent未决= PendingIntent.getActivity(contextA,0,intent2,0);
                    notify.setLatestEventInfo(contextA,标题,细节待定);
                    nm.notify(0,通知);
                  * /                        ///通知结束                    }
            }赶上(的URISyntaxException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(ClientProtocolException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(JSONException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
             //这里就结束了您的code             wl.release();         }     公共无效SetAlarm(上下文的背景下)
     {
         AlarmManager AM =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         意图I =新意图(背景下,Alarm.class);
         的PendingIntent圆周率= PendingIntent.getBroadcast(上下文,0,I,0);
         am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的(),8000,PI); //毫秒*秒*分 - 显然会增加至每天运行
     }     公共无效CancelAlarm(上下文的背景下)
     {
         意向意图=新意图(背景下,Alarm.class);
         的PendingIntent发件人= PendingIntent.getBroadcast(上下文,0,意图,0);
         AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(寄件人);
     }
 }


解决方案

检查:

 的通知的通知;
NotificationManager纳米;
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder;
私有静态最终诠释NOTIFICATION_ID = 1;
   @覆盖
    公共无效的onReceive(上下文的背景下,意图ARG1){
        showNotification(上下文);
    }    私人无效showNotification(上下文的背景下){
            乌里alarmSound = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            如果(alarmSound == NULL){
                alarmSound = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }            mBuilder =新NotificationCompat.Builder(本)
                    .setSmallIcon(R.drawable.image)
                    .setSound(alarmSound)
                    .setContentTitle(新位置找到)
                    .setContentText(
                            值
                                    +新位置已经分配给你);            mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            mBuilder.setAutoCancel(真);            意图resultIntent =新意图(这一点,HeliosActivity.class);
            resultIntent.putExtra(skiphomescreen,真正的);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(本);
            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());    }

I'm making an app that has a repeating alarm, that checks the date from an api, if the "date" is true, it will send a notification. The commented out notification is what I'm working with, as it works in My MainActivity class, I just can't get it notifying in my Alarm class. Any ideas?

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

解决方案

Check this :

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

    }  

这篇关于Android的从我的报警类别通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆