简体   繁体   English

ListView不会更新onNewIntent()

[英]ListView won't update onNewIntent()

I'm developing a quite simple application that receives GCM's push notifications. 我正在开发一个非常简单的应用程序,它可以接收GCM的推送通知。 There are similar questions here in SO, but this is the only one I found that tries to update the ListView in the onNewIntent() hook, but it wasn't helpful. 在SO中也有类似的问题,但这是我发现的唯一一个试图在onNewIntent()挂钩中更新ListViewonNewIntent() ,但这没有帮助。

The "business logic" should be: “业务逻辑”应为:

  • If a notification is received when the application isn't in foreground, add it to the ListView (so, the ListView will have only one item). 如果在应用程序不在前台时收到通知,则将其添加到ListView(因此,ListView将只有一项)。

  • If a notification is received when the application is in foreground, add it to the ListView (so, it will have n>1 items). 如果在应用程序处于前台时收到通知,请将其添加到ListView(因此,它将具有n> 1个项目)。

This is the Activity that holds the ListView : 这是保存ListViewActivity

package ar.edu.uca.ingenieria.notificaciones;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ar.edu.uca.ingenieria.notificaciones.adapter.NotificacionesAdapter;
import ar.edu.uca.ingenieria.notificaciones.gcm.GcmIntentService;
import ar.edu.uca.ingenieria.notificaciones.model.Notificacion;

/**
 * Main UI for the demo app.
 */
public class MainActivity extends Activity {

    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private static final String TAG = "GCM Demo";

    /**
     * Es el project number obtenido en la API Console, como se explica en "Getting Started."
     */
    String senderId;

    TextView mDisplay;
    List<Notificacion> notificaciones;
    private ListView notificacionesListView;

    GoogleCloudMessaging gcm;
    Context context;
    String regid;
    private NotificacionesAdapter notificacionesAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mDisplay = (TextView) findViewById(R.id.display);

        context = getApplicationContext();
        senderId = getSenderId();
        intentarRegistrarGooglePlayServices();

        Notificacion notificacion = getNotificacionFromIntent();
        this.notificaciones = new ArrayList<Notificacion>();
        inicializarListView(notificacion);
    }

    private void inicializarListView(Notificacion notificacion) {
        this.notificaciones.add(notificacion);
        // Create the adapter to convert the array to views
        notificacionesAdapter = new NotificacionesAdapter(this, this.notificaciones);
        // Attach the adapter to a ListView
        this.notificacionesListView = (ListView) findViewById(R.id.lista_notificaciones);
        this.notificacionesListView.setAdapter(notificacionesAdapter);
    }

    private void agregarNotificacion(Notificacion notificacion) {
        this.notificaciones.add(notificacion);
        // this.notificacionesAdapter.add(notificacion);
        this.notificacionesAdapter.notifyDataSetChanged();
    }

    /**
     * Verifica si Google Play Services está instalado en el dispositivo. De ser así, intenta
     * registrarse y guarda el regid obtenido.
     */
    private void intentarRegistrarGooglePlayServices() {
        // Check device for Play Services APK. If check succeeds, proceed with GCM registration.
        if (checkPlayServices()) {
            gcm = GoogleCloudMessaging.getInstance(this);
            regid = getRegistrationId(context);

            if (regid.isEmpty()) {
                registerInBackground();
            }
        } else {
            Log.i(TAG, "No valid Google Play Services APK found.");
        }
        Log.i(TAG, "regid: " + this.regid);
    }

    private Notificacion getNotificacionFromIntent() {
        String tituloNotificacion = (String) getIntent().getCharSequenceExtra(GcmIntentService.GCM_TITULO);
        Log.d(TAG, "Título: " + tituloNotificacion);
        String mensajeNotificacion = (String) getIntent().getCharSequenceExtra(GcmIntentService.GCM_MENSAJE);
        Log.d(TAG, "Mensaje: " + mensajeNotificacion);
        // TODO esto muestra "1/1/1970" si no viene la fecha...
        long fechaMilisegundos = getIntent().getLongExtra(GcmIntentService.GCM_FECHA, new Date(0).getTime());
        Date fechaNotificacion = new Date(fechaMilisegundos);
        return new Notificacion(tituloNotificacion, mensajeNotificacion, fechaNotificacion);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Check device for Play Services APK.
        checkPlayServices();
        Log.i(TAG, "regid: " + this.regid);
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent");
        this.setIntent(intent);
        this.agregarNotificacion(this.getNotificacionFromIntent());
    }

    /**
     * Check the device to make sure it has the Google Play Services APK. If
     * it doesn't, display a dialog that allows users to download the APK from
     * the Google Play Store or enable it in the device's system settings.
     */
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
    }

    /**
     * Stores the registration ID and the app versionCode in the application's
     * {@code SharedPreferences}.
     *
     * @param context application's context.
     * @param regId registration ID
     */
    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGcmPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PROPERTY_REG_ID, regId);
        editor.putInt(PROPERTY_APP_VERSION, appVersion);
        editor.apply();
    }

    /**
     * Gets the current registration ID for application on GCM service, if there is one.
     * <p>
     * If result is empty, the app needs to register.
     *
     * @return registration ID, or empty string if there is no existing
     *         registration ID.
     */
    private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGcmPreferences(context);
        String registrationId = prefs.getString(PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing regID is not guaranteed to work with the new
        // app version.
        int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }

    /**
     * Registers the application with GCM servers asynchronously.
     * <p>
     * Stores the registration ID and the app versionCode in the application's
     * shared preferences.
     */
    private void registerInBackground() {
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                String msg = "";
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(context);
                    }
                    regid = gcm.register(senderId);
                    msg = "Device registered, registration ID=" + regid;

                    // You should send the registration ID to your server over HTTP, so it
                    // can use GCM/HTTP or CCS to send messages to your app.
                    sendRegistrationIdToBackend();

                    // For this demo: we don't need to send it because the device will send
                    // upstream messages to a server that echo back the message using the
                    // 'from' address in the message.

                    // Persist the regID - no need to register again.
                    storeRegistrationId(context, regid);
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                    // If there is an error, don't just keep trying to register.
                    // Require the user to click a button again, or perform
                    // exponential back-off.
                }
                return msg;
            }

            @Override
            protected void onPostExecute(String msg) {
                mDisplay.append(msg + "\n");
            }
        }.execute(null, null, null);
    }

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

    /**
     * @return Application's version code from the {@code PackageManager}.
     */
    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);
        }
    }

    /**
     * @return Application's {@code SharedPreferences}.
     */
    private SharedPreferences getGcmPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but
        // how you store the regID in your app is up to you.
        return getSharedPreferences(MainActivity.class.getSimpleName(),
                Context.MODE_PRIVATE);
    }

    // TODO implementar esto del lado del server
    /**
     * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP or CCS to send
     * messages to your app. Not needed for this demo since the device sends upstream messages
     * to a server that echoes back the message using the 'from' address in the message.
     */
    private void sendRegistrationIdToBackend() {
        // Your implementation here.
    }

    // TODO: esto no es responsabilidad de esta clase...
    // Tal vez un "setting provider" o similar
    private String getSenderId() {
        Resources res = getResources();
        return res.getString(R.string.senderId);
    }

}

However, although the agregarNotificacion() method is called with the correct notification, it won't update the ListView items. 但是,尽管使用正确的通知调用了agregarNotificacion()方法,但它不会更新ListView项。 I did quite a bit googling, and read that methods like myAdapter.add() should be used, but it didn't work either (that's why I left it commented). 我做了很多的谷歌搜索,并阅读了应该使用诸如myAdapter.add()之类的方法,但是它也不起作用(这就是为什么我将其置为注释)。

So, in this particular situation (creating the ListView in one Activity hook, and updating it in another), how could I refresh my ListView? 因此,在这种特殊情况下(在一个Activity钩子中创建ListView,并在另一个钩子中对其进行更新),如何刷新ListView?

Two final considerations: 最后两个注意事项:

  1. There's a small bug: if the application is run for the first time, and a push is received then, it won't be added (since the getNotificacionFromIntent() method is returning an object with null fields, instead of a null Notificacion ). 有一个小错误:如果应用程序是第一次运行,并且接收到推送,则不会添加它(因为getNotificacionFromIntent()方法返回的是具有空字段的对象,而不是空的Notificacion )。 I'm aware of it, but running and exiting the app is a quick possible workaround. 我知道,但是运行和退出该应用程序是一种快速的解决方法。
  2. Sorry for the "spanglish": this is a university project, so I'm using Spanish names in the variables and classes. 抱歉,“ spanglish”:这是一个大学项目,所以我在变量和类中使用西班牙语名称。 But also, there's some copy-paste involved, that I haven't refactored yet. 但是,还涉及一些复制粘贴,我还没有重构。

Any help or direction will be highly appreciated. 任何帮助或指示将不胜感激。 Regards. 问候。

解决方案是从ListActivity扩展,并通过以下this.notificacionesAdapter.add(newItem)发送新项目: this.notificacionesAdapter.add(newItem)

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

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