【发布时间】:2015-01-22 15:57:11
【问题描述】:
我有一个应用程序可以从服务器接收推送通知,而且效果很好。 但是不起作用的是,如果我不打开通知,并且下一个通知到达,则上一个会丢失。
我想实现,我的所有通知都被保存,当我打开该活动时,我可以通过它们。
我设置了 SharedPreferences 和几个 textViews,但它们只显示我打开的最后一个。
我正在考虑 ListView,并创建一些本地基地,我可以在其中保存传入的消息,但我不知道我应该朝哪个方向移动。
我还必须说,我对 Android 编程很陌生,也许这是一些简单的东西,但我错过了。
我会很感激任何指导方针或代码修正
这是我的代码示例(来自 GitHub,稍作修改):
package com.example.scratchtest;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView.FindListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.androidquery.AQuery;
import com.example.scratchtest.Splash.GlobalVariables;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class GreetingActivity extends Activity {
TextView emailET;
TextView emailET2;
TextView emailET3;
private AQuery aq;
// Progress Dialog bar object
ProgressDialog prgDialog;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
// test za push//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_greeting);
aq = new AQuery(this);
String json = getIntent().getStringExtra("greetjson");
SharedPreferences prefs = getSharedPreferences("UserDetails",
Context.MODE_PRIVATE);
emailET = (TextView) findViewById(R.id.greetingmsg);
emailET2 = (TextView) findViewById(R.id.greetingmsg2);
emailET3 = (TextView) findViewById(R.id.greetingmsg3);
// Check if Google Play Service is installed in Device
// Play services is needed to handle GCM stuffs
if (!checkPlayServices()) {
Toast.makeText(
getApplicationContext(),
"This device doesn't support Play services, App will not work normally",
Toast.LENGTH_LONG).show();
}
// When json is not null
if (json != null) {
try {
// View ListView = findViewById(R.id.listView1);
JSONObject jsonObj = new JSONObject(json);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("greetimgurl", jsonObj.getString("greetImgURL"));
editor.putString("greetmsg", jsonObj.getString("greetMsg"));
editor.commit();
emailET.setText(prefs.getString("greetmsg", ""));
// Render Image read from Image URL using aquery 'image' method
aq.id(R.id.greetimg)
.progress(R.id.progress)
.image(prefs.getString("greetimgurl", ""), true, true,
0, 0, null, AQuery.FADE_IN);
if (emailET.getText().toString().trim().length() == 0);
emailET2.setText(prefs.getString("greetmsg", ""));
if (emailET2.getText().toString().trim().length() == 0)
emailET3.setText(prefs.getString("greetmsg", ""));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// When Json is null
else if (!"".equals(prefs.getString("greetimgurl", "")) && !"".equals(prefs.getString("greetmsg", "") != null)) {
emailET.setText(prefs.getString("greetmsg", ""));
aq.id(R.id.greetimg)
.progress(R.id.progress)
.image(prefs.getString("greetimgurl", ""), true, true, 0,
0, null, AQuery.FADE_IN);
}
}
// Check if Google Playservices is installed in Device or not
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
// When Play services not found in device
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
// Show Error dialog to install Play services
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(
getApplicationContext(),
"This device doesn't support Play services, App will not work normally",
Toast.LENGTH_LONG).show();
finish();
}
return false;
} else {
Toast.makeText(
getApplicationContext(),
"This device supports Play services, App will work normally",
Toast.LENGTH_LONG).show();
}
return true;
}
// When Application is resumed, check for Play services support to make sure
// app will be running normally
@Override
protected void onResume() {
super.onResume();
checkPlayServices();
}
}
【问题讨论】:
标签: java android eclipse push-notification sharedpreferences