简体   繁体   English

每次启动应用程序时,AlarmManager都在工作

[英]AlarmManager is working every time the application starts

I have been searching 3 days for this. 我一直在搜索3天。 I tried every single solution out there in google and stackoverflow.com . 我在googlestackoverflow.com中尝试了每种解决方案。

I have a MainActivity which starts a BroadcastReceiver like this. 我有一个MainActivity ,它可以像这样启动BroadcastReceiver

Calendar calendar3 = Calendar.getInstance();

    calendar3.set(Calendar.HOUR_OF_DAY, 7);
    calendar3.set(Calendar.MINUTE, 34);
    calendar3.set(Calendar.SECOND, 00);
    Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent);

i tried alarmManager.setReapeating too with no luck. 我尝试过alarmManager.setReapeating也没有运气。

Then the BroadcastReceiver starts a service which gets some info from db and put it in a notification or more like this: 然后, BroadcastReceiver启动一个service ,该service从db获取一些信息,并将其放入notification或更像这样:

Intent i = new Intent(context,MyService.class);
context.startService(i);

the service 服务

public class MyService extends Service {
MyDB db;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("Yamen", "service Started");
    db = new MyDB(getApplicationContext());
    GregorianCalendar calendar = new GregorianCalendar();
    Date time = calendar.getTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String timeString = simpleDateFormat.format(time);
    SharedPreferences mySharedPreferences = getApplicationContext().getSharedPreferences("myPref",Context.MODE_PRIVATE);
    boolean isNoti=mySharedPreferences.getBoolean("isNoti",true);
    if (isNoti) {
        ArrayList<AnniBean> annis =
                db.getAnni(timeString);
        Log.e("Yamen", "size is " + annis.size());
        for (AnniBean anni : annis) {
            new NotificationCreator(getApplicationContext(), "Anniversary Reminder", anni.getAnniName(), anni.getAnniDetails());
        }
    }
    Log.e("Yamen", "service finished");
    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

notification appears everyday at 7:34 am like a charm. notification每天早上7:34出现,就像一种魅力。 The problem is the BroadcastReceiver and the service are being called every time i open the application too. 问题是每次我也打开应用程序时,也会调用BroadcastReceiverservice

please don't refer me to any link because I did visited a lot of links on this. 请不要将我引至任何链接,因为我确实访问了许多链接。 Thanks in advice 谢谢建议

The problem is in the Broadcast receiver intent XML. 问题出在广播接收者意图XML中。 Its receiving other broadcasts, you should filter by action like this: In Java: 它接收其他广播时,应按以下操作进行过滤:在Java中:

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
intent.setAction("your.package.name.your.anything");

in XML: 在XML中:

<receiver android:name=".MyBroadcastReceiver"> 
<intent-filter> 
        <action android:name="your.package.name.your.anything" />  
</intent-filter> 
</receiver>

Try with following code: 尝试以下代码:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 34);
cal.set(Calendar.SECOND, 0);

if(cal.before(Calendar.getInstance())){ // if it's in the past, increment
    cal.add(Calendar.DATE, 1);
}

Intent intent = new Intent(MainAcitivty.this, BroadCastReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainAcitivty.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

This alarm will run every day at specified time. 该警报将每天在指定时间运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM