我有一个每 5 分钟运行一次的 IntentService。此服务检查服务器上是否有任何警报。警报位于二维数组列表中,我将其传递给 Activity 以显示在列表中。
目前,如果我启动该应用程序并让它运行一个小时,堆栈中将有 12 个 Activity。用户必须单击后退按钮 12 次才能关闭所有 Activity 。
我希望发生的事情是将新的 ArrayList 传递给堆栈上的原始 Activity,并使用新数据更新它的 listView。
我确定我必须覆盖 onNewIntent 并在适配器上调用 notifyDataSetChanged。
有没有人有任何想法如何做到这一点? 我在正确的路线上吗?
public class ShowAlertsIntentService extends IntentService{
private static final String TAG = ShowAlertsIntentService.class.getSimpleName();
RROnCallApplication rrOnCallApp;
DateTime from;
DateTime to;
TwoDimensionalArrayList<String> alertsArray;
public ShowAlertsIntentService() {
super("ShowAlertsIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, "inside onHandleIntent of ShowAlertsIntentService");
rrOnCallApp = (RROnCallApplication) getApplication();
from = new DateTime();
to = from.plusDays(1);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedfrom = fmt.print(from);
String formattedTo = fmt.print(to);
alertsArray = rrOnCallApp.webService.getAlerts("1", formattedfrom, formattedTo);
if(alertsArray != null){
Log.e(TAG, "size of alertArray = " + alertsArray.size());
String noCallsStatus = null;
String outOfRangeStatus = null;
ArrayList arrayList = (ArrayList) alertsArray.get(0);
noCallsStatus = (String) arrayList.get(0);
if((alertsArray.size() == 1) && (noCallsStatus.equalsIgnoreCase("no sig") || noCallsStatus.equalsIgnoreCase("no data"))){
//do nothing
Log.e(TAG, "no data or no sig sent back when getting alerts");
}else{
Intent intentShowAlertsActivity = new Intent(this, ShowAlertsActivity.class);
Bundle b = new Bundle();
b.putSerializable("alertsarray", alertsArray);
intentShowAlertsActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentShowAlertsActivity.putExtra("alertsarraybundle", b);
startActivity(intentShowAlertsActivity);
}
}else{
Log.e(TAG, "alertsArray = null!!!!!");
}
}
}
.
public class AlertsFragment extends ListFragment{
private static final String TAG = AlertsFragment.class.getSimpleName();
MySimpleArrayAdapter myAdapter;
ArrayList<String> alertsArray;
TextView alertTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getArguments();
if(b != null){
alertsArray = (ArrayList<String>) b.get("alertsArray");
Log.e(TAG, "alertsArray in AlertsFragment has size " + alertsArray.size());
}else{
Log.e(TAG, "Bundle b = null!!!!!!!!!!!");
}
}//end of onCreate
. [编辑1]
07-07 15:46:26.056: E/AndroidRuntime(8253): FATAL EXCEPTION: IntentService[ShowAlertsIntentService]
07-07 15:46:26.056: E/AndroidRuntime(8253): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.app.ContextImpl.startActivity(ContextImpl.java:864)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
07-07 15:46:26.056: E/AndroidRuntime(8253): at com.carefreegroup.rr3.carefreeoncall.ShowAlertsIntentService.onHandleIntent(ShowAlertsIntentService.java:90)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.Looper.loop(Looper.java:137)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.HandlerThread.run(HandlerThread.java:60)
.
[编辑 2]
<activity
android:name=".ShowAlertsActivity"
android:screenOrientation="landscape" />
编辑3
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(TAG, "Inside onNewIntent");
alertsArray = (ArrayList<String>) getIntent().getBundleExtra("alertsarraybundle").get("alertsarray");
Log.e(TAG, "size of alertArray in ShowAlertsActivity = " + alertsArray.size());
Bundle b = new Bundle();
b.putSerializable("alertsArray", alertsArray);
Fragment newFragment = new AlertsFragment();
newFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.showalertslistfragment_container, newFragment);
//transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
最佳答案
为 Android list 中的 Activity 使用 android:launchMode="singleInstance"
。这将确保仅维护 Activity 的单个实例,并且用户不必单击 12 次。
查看此答案以获取更多详细信息:
Android singleTask or singleInstance launch mode?
编辑 1:如果您的 Activity 已经在运行,您将在 onNewIntent() 方法中收到新的 Intent 。您可以在那里刷新您的列表。
关于Android 如何在 Activity 已经运行时将数组从 IntentService 传递给 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24613214/