报警自动重复

时间:2015-05-29 07:41:31

标签: android broadcastreceiver android-service android-alarms

我在特定用户输入的时间响铃。问题是,经过一段时间后,即使没有设定时间,警报也会再次响起。在一段固定时间后,此重复警报不会响起。有时它会在2分钟后响起,有时会在7分钟后响起等等。我不想这样,它毁了我的项目。以下是我的代码,你能帮助我吗?

   Intent myIntent = new Intent(ReminderService.this, MyReceiver.class);
            int randomPIN = (int)(Math.random()*9000)+1000;
            pendingIntent = PendingIntent.getBroadcast(ReminderService.this, randomPIN, myIntent,pendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() , pendingIntent);

MyReceiver.java:

  package app.aguai.medieazy;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{

@Override
 public void onReceive(Context context, Intent intent)
{
   Intent service1 = new Intent(context, MyAlarmService.class);
   context.startService(service1);

 }

}

和我的AlarmService.java:

public class MyAlarmService extends Service 

{
 private NotificationManager mManager;

 @Override
 public IBinder onBind(Intent arg0)
 {
   // TODO Auto-generated method stub
    return null;
 }

@Override
public void onCreate() 
{
   // TODO Auto-generated method stub  
   super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
   super.onStart(intent, startId);

   Calendar c = Calendar.getInstance(); 
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int min=c.get(Calendar.MINUTE);
    int sec=c.get(Calendar.SECOND);
    String currenttime= String.valueOf(hour)+" : "+String.valueOf(min)+" : "+String.valueOf(sec);

   PrescribedDB db=new PrescribedDB(getApplicationContext());
   Intent i=new Intent(MyAlarmService.this,ReminderPopUp.class);
   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

   i.putExtra("name",db.getMed(currenttime));
   i.putExtra("time",currenttime);
   startActivity(i);
}

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

我明天必须启动这个项目,这是我的大项目中唯一的小错误。请帮忙。感谢名单。

0 个答案:

没有答案