待定意图导致mainactivity重新加载,这是为什么? [英] Pending Intent Causing mainactivity to reload, why is that?

查看:110
本文介绍了待定意图导致mainactivity重新加载,这是为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一项服务,该服务将在每个午夜检查服务器中的新数据并将其下载.

I'm trying to write a service that will check every midnight for new data from the server and will download it.

但是,当我启动该应用程序时,mainActivity屏幕会在几秒钟后重新加载. 我很高兴,它的发生是因为这项服务, 为什么会这样?

But when i start the app the mainActivity screen reloads after few seconds. I'v checed it and it happens because of this service, Why is this happening?

这些是文件:

MainActivity:我创建了一个AlarmManager对象来设置未决的Intent:

MainActivity: i'v created an AlarmManager object to set pendingIntent:

//Set alarm
    /* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(getApplicationContext(), AlarmReciever.class);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 1000 * 24 * 60 * 60;

    /* Set the alarm to start at 10:30 AM */
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 00);
    calendar.set(Calendar.MINUTE, 00);
    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

AlarmReciever:

public class AlarmReciever extends BroadcastReceiver {
    private Data newData = null;
    public SharedPreferences settings;
    ConnectivityManager cm = null;
    NetworkInfo netInfo = null;

    @Override
    public void onReceive(Context context, Intent intent) {

        newData = new Data(context);

        // TODO Auto-generated method stub
        newData.cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        newData.netInfo = newData.cm.getActiveNetworkInfo();
        newData.settings = PreferenceManager.getDefaultSharedPreferences(context);
//        System.out.print("-----------------" + newData.netInfo);
        newData.checkOnline();
    }
}

Data.java:

    public void checkOnline(){
    if (isOnline()){
        System.out.print("**************** YES Internet");
        firstAsyncTask task = new firstAsyncTask(this);
        try {
            Object dobj = task.execute("par1", "par 2", "par 3").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }else{
        System.out.print("**************** NO Internet");
    }
}

data.java文件很大,可以在此处发布,但是似乎在导致应用程序重新加载MainActivity页面的"checkOnline"方法中,我应该以其他方式发送服务吗?

The data.java file is to big to post in here, but it seems that the "checkOnline" method in in causing the app to reload the MainActivity page, should i send the service differently?

感谢阅读和阅读回答.

推荐答案

在您的Activity中,您可以执行以下操作:

In your Activity you do this:

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

这将导致立即触发BroadcastReceiver,因为您已将System.currentTimeMillis()指定为首次触发的时间.

This causes the BroadcastReceiver to be triggered immediately, since you have specified System.currentTimeMillis() as the time to trigger the first time.

您可能打算第一次使用calendar.getTimeInMillis()来触发警报.但是即使您将其更改为,它仍然会立即触发,因为您已将日历中的时间设置为当日的时间,该时间已经通过了!您需要使用calendar.getTimeInMillis() + interval(这是后续日期的00:00),也可以在使用calendar.getTimeInMillis()之前将1天添加到日历中.

You probably mean to use calendar.getTimeInMillis() as the first time to trigger the alarm. But even if you change it to that, it will still trigger immediately because you've set the time in your calendar to 00:00 of the current day, which has already passed! You need to either use calendar.getTimeInMillis() + interval (which would be 00:00 of the following day, or you can add 1 day to your calendar before using calendar.getTimeInMillis().

这篇关于待定意图导致mainactivity重新加载,这是为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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