简体   繁体   English

待定意图导致mainactivity重新加载,为什么?

[英]Pending Intent Causing mainactivity to reload, why is that?

I'm trying to write a service that will check every midnight for new data from the server and will download it. 我正在尝试编写一项服务,该服务将在每个午夜检查服务器中的新数据并将其下载。

But when i start the app the mainActivity screen reloads after few seconds. 但是,当我启动该应用程序时,mainActivity屏幕会在几秒钟后重新加载。 I'v checed it and it happens because of this service, Why is this happening? 我为此感到高兴,它是由于这项服务而发生的,为什么会这样呢?

Her are the files: 她是文件:

MainActivity: i'v created an AlarmManager object to set pendingIntent: MainActivity:我创建了一个AlarmManager对象来设置未决的意图:

//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: 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: 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");
    }
}

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? data.java文件很大,可以在这里发布,但是似乎在导致应用程序重新加载MainActivity页面的“ checkOnline”方法中,我应该以其他方式发送服务吗?

Thanx for reading & answering. 感谢阅读和回答。

Looks like you have written this line by mistake 看来您写错了这行

pendingIntent = PendingIntent.getService(getApplicationContext(), 0, alarmIntent, 0);

It should be like this 应该是这样

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);

Because you are using BroadcastReciever. 因为您正在使用BroadcastReciever。

Causes the following line network access? 导致以下线路网络访问?

Object dobj = task.execute("par1", "par 2", "par 3").get();

If so then the system might kill your process (ether for networking on main thread or for event loop timeout aka. ANR). 如果是这样,那么系统可能会杀死您的进程(用于在主线程上联网或用于事件循环超时(也称为ANR)的以太网)。 And eventually restart it again if its a service. 并最终重新启动它(如果它有服务)。

In your Activity you do this: 在您的Activity您可以执行以下操作:

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

This causes the BroadcastReceiver to be triggered immediately, since you have specified System.currentTimeMillis() as the time to trigger the first time. 由于您已将System.currentTimeMillis()指定为首次触发时间,因此这将导致立即触发BroadcastReceiver

You probably mean to use calendar.getTimeInMillis() as the first time to trigger the alarm. 您可能打算将calendar.getTimeInMillis()用作第一次触发警报。 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! 但是,即使将其更改为该日期, 它仍然会立即触发,因为您已将日历中的时间设置为当天的 00:00,该日期已经过去了! 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() . 您需要使用calendar.getTimeInMillis() + interval (将是第二天的 00:00,或者可以在使用calendar.getTimeInMillis()之前向日历添加1天。

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

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