在android中设置不同时间的通知

在android中设置不同时间的通知,android,Android,我有一个android应用程序。我想在用户设置的五个不同时间生成五个通知。如果我的应用程序无法运行,则应生成通知…我如何才能做到这一点 在下面的类中,我将把db通知值与oncreate()中无限while循环中的设备时间进行比较。res1、res2…res5是存储在db中的五个通知时间。day4是系统时间 MyService.java ppublic class MyService extends Service { private static final Stri

我有一个android应用程序。我想在用户设置的五个不同时间生成五个通知。如果我的应用程序无法运行,则应生成通知…我如何才能做到这一点

在下面的类中,我将把db通知值与oncreate()中无限while循环中的设备时间进行比较。res1、res2…res5是存储在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");
      }
      }
          }
pppublic类MyService扩展服务{
私有静态最终字符串数据库\u NAME=“MYPRAYER.db”;
私有静态最终字符串数据库\u TABLE=“notification”;
String tag=“TestService”;
字符串a=“你好”;
字符串res1、res2、res3、res4、res5;
整小时,分钟;
字符串day3,day4,minute1,hour1;
媒体播放器;
/*服务创建*/
@凌驾
public void onCreate(){
super.onCreate();
Toast.makeText(此“服务已创建…”,Toast.LENGTH_LONG.show();
Log.i(标记“服务已创建…”);
/*从数据库通知中重新接收通知时间*/
sqlitemydb数据库;
myDB=openOrCreateDatabase(数据库名称,Context.MODE\u PRIVATE,null);
String[]resultColumns=新字符串[]{“fajr”、“zuhr”、“asr”、“magrib”、“isha”};
Cursor allRows=myDB.query(数据库\表,结果列,null,null,null,null,null);
整数cindex=allRows.getColumnIndex(“fajr”);
整数cindex1=allRows.getColumnIndex(“zuhr”);
整数cindex2=allRows.getColumnIndex(“asr”);
整数cindex3=allRows.getColumnIndex(“magrib”);
整数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();
而(!Thread.currentThread().isInterrupted())
{
尝试
{
Calendar cal=Calendar.getInstance();//重新接收日历日期
hour=cal.get(Calendar.hour OF_DAY);//从日历中提取小时
分钟=cal.get(Calendar.minute);//从日历中提取分钟
分钟1=分钟+“”;//将分钟转换为数据环
如果(分钟1等于(“1”))
{
分钟1=“01”;
}
else if(分钟1等于(“2”))
{
分钟1=“02”;
}
如果(分钟1等于(“3”))
{
分钟1=“03”;
}
如果(分钟1等于(“4”))
{
分钟1=“04”;
}
else if(分钟1等于(“5”))
{
分钟1=“05”;
}
else if(分钟1等于(“6”))
{
分钟1=“06”;
}
else if(分钟1等于(“7”))
{
分钟1=“07”;
}
else if(分钟1等于(“8”))
{
分钟1=“08”;
}
如果(分钟1等于(“9”))
{
分钟1=“09”;
}
/*转换为12小时格式*/
如果(小时<12)
{
小时=小时;
第3天=小时+“+”:“+分钟1;
day4=day3++“AM”//day4包含系统时间
}
其他的
{
小时=12小时;
第3天=小时+“+”:“+分钟1;
第4天=第3天+“+”下午;
}
如果(第4天等于(res1))
{
通知(“通知标题”、“通知信息”);
}
如果(第4天等于(res2))
{
通知(“通知标题”、“通知信息”);
}
如果(第4天等于(第3天))
{
通知(“通知标题”、“通知信息”);
} 
如果(第4天等于(第4天))
{
通知(“通知标题”、“通知信息”);
}
如果(第4天等于(第5天))
{
通知(“通知标题”、“通知信息”);
}
睡眠(1000);
} 
捕捉(中断异常e)
{
Thread.currentThread().interrupt();
//System.out.println(“此处的新1异常”);
}
捕获(例外e)
{
//Thread.currentThread().interrupt();
System.out.println(“此处的新异常”);
}          
}
}         
@凌驾
公共void onStart(Intent Intent,int startId){
super.onStart(intent,startId);
Log.i(标记“服务已启动…”);
}
@凌驾
公共空间{
super.ondestory();
Toast.makeText(此“服务已销毁…”,Toast.LENGTH_LONG.show();
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
私有无效通知(字符串notificationTitle、字符串notificationMessage)
{
NotificationManager NotificationManager=(NotificationManager)getSystemService(通知服务);
通知不