【问题标题】:Setting notifications at different times in android在android中设置不同时间的通知
【发布时间】:2012-07-28 18:13:59
【问题描述】:

我有一个 android 应用程序。我想在用户设置的五个不同时间生成五个通知。如果我的应用程序无法运行,则应该生成通知...我该怎么做?

在下面的类中,我在 oncreate().res1,res2...res5 中将 db 通知值与设备时间进行比较,这是存储在 db.day4 中的五个通知时间是系统时间...

MyService.java

ppublic class MyService extends Service {
              private static final String DATABASE_NAME = "MYPRAYER.db";   
              private static final String DATABASE_TABLE = "notification";
              String tag="TestService";
                String a="hello";
                String res1,res2,res3,res4,res5;
                int hour,minute;
                String day3,day4,minute1,hour1;
                MediaPlayer mMediaPlayer;
                   /* Service creation */
               @Override
               public void onCreate() {
             super.onCreate();
               Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
               Log.i(tag, "Service created...");


                  /* Rereive notification times from the databse notificaion */
              SQLiteDatabase myDB;
            myDB = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
            String[] resultColumns = new String[] {"fajr","zuhr","asr","magrib","isha"};
            Cursor allRows = myDB.query(DATABASE_TABLE, resultColumns, null, null, null, null, null, null);

            Integer cindex = allRows.getColumnIndex("fajr");
            Integer cindex1 = allRows.getColumnIndex("zuhr");
            Integer cindex2 = allRows.getColumnIndex("asr");
            Integer cindex3 = allRows.getColumnIndex("magrib");
            Integer cindex4 = allRows.getColumnIndex("isha");
            allRows.moveToFirst();
            res1=allRows.getString(cindex);
            res2=allRows.getString(cindex1);
            res3=allRows.getString(cindex2);
            res4=allRows.getString(cindex3);
            res5=allRows.getString(cindex4);


            myDB.close();

            while(!Thread.currentThread().isInterrupted())
            {
            try 
            {

                Calendar cal = Calendar.getInstance(); // Rereive calender date
                hour = cal.get(Calendar.HOUR_OF_DAY); // Take hour from the calender
                minute = cal.get(Calendar.MINUTE);    // Take minute from the calender
                minute1=minute + "";  // Convert minute to dtring
                if(minute1.equals("1")) 
                {
                minute1="01";
                }
                else if(minute1.equals("2"))
                {
                minute1="02";
                }
                else if(minute1.equals("3"))
                {
                minute1="03";
                }
                else if(minute1.equals("4"))
                {
                minute1="04";
                }
                else if(minute1.equals("5"))
                {
                minute1="05";
                }
                else if(minute1.equals("6"))
                {
                minute1="06";
                }
                else if(minute1.equals("7"))
                {
                minute1="07";
                }
                else if(minute1.equals("8"))
                {
                minute1="08";
                }
                else if(minute1.equals("9"))
                {
                minute1="09";
                }
                     /* Converting to 12 hour format */
                if ( hour < 12 )
                {
                     hour=hour;
                     day3=hour + "" + ":" + minute1;
                     day4=day3 + " " + "AM"; // DAY4 Contains the system time
                }
                else
                {
                    hour=hour-12;
                    day3=hour + "" + ":" + minute1;
                    day4=day3 + " " + "PM";
                }



                if(day4.equals(res1))
              {
                     Notification("Notification Title","Notification Message");


              }



                 if(day4.equals(res2))
                  {
                         Notification("Notification Title","Notification Message");

                  }


                 if(day4.equals(res3))
                  {
                         Notification("Notification Title","Notification Message");

                  } 
                 if(day4.equals(res4))
                  {
                         Notification("Notification Title","Notification Message");

                  }
                 if(day4.equals(res5))
                  {
                         Notification("Notification Title","Notification Message");

                  }

               Thread.sleep(1000);
       } 
       catch (InterruptedException e)
       {
               Thread.currentThread().interrupt();
               //System.out.println("New 1 Exception here");

       }

       catch (Exception e)
      {
               //Thread.currentThread().interrupt();
           System.out.println("nEW Exception here");
      }          
       }

       }         
          @Override
          public void onStart(Intent intent, int startId) {      
          super.onStart(intent, startId);  
          Log.i(tag, "Service started...");
       }
         @Override
         public void onDestroy() {
         super.onDestroy();
         Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
       }

         @Override
         public IBinder onBind(Intent intent) {
         return null;
        }

         private  void Notification(String notificationTitle, String notificationMessage)
       {
         NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         Notification notification = new Notification(R.drawable.ic_launcher, "A New Message!", System.currentTimeMillis());

         Intent notificationIntent = new Intent(this,MyService.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

         notification.setLatestEventInfo(MyService.this, notificationTitle, notificationMessage, pendingIntent);
         notificationManager.notify(10001, notification);
   //HELLO_ID++;
         Uri alert = RingtoneManager.getDefaultUri(
        RingtoneManager.TYPE_NOTIFICATION); 

        mMediaPlayer = new MediaPlayer();
      try
      {
      mMediaPlayer.setDataSource(this, alert);

      AudioManager audioManager = (AudioManager)getSystemService(
                                  Context.AUDIO_SERVICE);
      int volumen = audioManager.getStreamVolume(
                    AudioManager.STREAM_NOTIFICATION);

      if (volumen != 0) {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
      }
      }
      catch(IOException e)
      {  
          System.out.println("Exception here");
          //System.out.println("Exception 1 here");
      }
      }
          }

Logcat:

07-27 16:25:00.542: I/TestService(363): 服务创建... 07-27 16:25:20.752: I/dalvikvm(363): threadid=7: 对信号 3 做出反应 07-27 16:25:20.803: I/dalvikvm(363): 将堆栈跟踪写入'/data/anr/traces.txt'

【问题讨论】:

    标签: android


    【解决方案1】:

    我所做的是使用AlarmManager 触发BroadcastReceiver 来设置通知。所以在 Android 文档上搜索。也有很多关于这些东西的资源。其他人也可能会告诉您不要使用BroadcastReceiver,而是使用Service。老实说,它可以双向进行,所以无论您做出什么选择都应该没问题。至于通知,您可以轻松地谷歌通知以查找相关资源。搜索状态通知。

    【讨论】:

    • 我已经运行了我的应用程序。通过将数据库存储的值与设备时间进行比较,我的应用程序中的通知被安排在服务中......但通知运行不正常......设备只是强制关闭几秒钟后。但是通知是用 forceclose 播放的......我有什么问题......
    • 您需要发布错误内容。除非我看到,否则我无能为力。如果我看到你的类实现了Service,这也会有所帮助。
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2017-10-16
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多