我想寫一個服務,將檢查每個午夜從服務器的新數據,並將下載它。Pending Intent使主要活動重新加載,爲什麼?
但是,當我啓動應用程序mainActivity屏幕後幾秒鐘重新加載。 我爲此付出了代價, 爲什麼會發生這種情況?
她是文件:
MainActivity:我心中已經創建了一個AlarmManager對象設置的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」的方法,我應該送不同的服務?
感謝Thanx &回答。
對不起,我修好了,它仍然在發生 – Eli
根據你的代碼,不知道爲什麼會發生這種情況 –