[英]Using gcm to update an ImageView in a ListView
So here is my problem, what I'm trying to do is the following: 所以这是我的问题,我想要做的是以下内容:
I have a simple ListView class that gets the item info (name of the item) from a MySql DB via PHP script and that works fine. 我有一个简单的ListView类,该类通过PHP脚本从MySql DB获取项目信息(项目名称),并且工作正常。
The other part of the ListView item is a ImageView. ListView项目的另一部分是ImageView。 It represents 3 states the item can be in. A picture of a green "traffic light" means 'its OK', a red one, its 'not OK' and a grey one means 'service not available'.
它表示该项目可以处于的3个状态。绿色“交通灯”的图片表示“正常”,红色表示“不正常”,灰色表示“服务不可用”。 The state change trigger comes from an other app (via gcm) and the message contains a value of -1, 0 or 1 which I want to use to trigger the ImageView changes.
状态更改触发器来自另一个应用程序(通过gcm),并且消息包含值-1、0或1,我想使用该值来触发ImageView更改。
Here is my onCreate method for my Activity: 这是我的Activity的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel_list);
mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences
.getBoolean(SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
Log.i(TAG, getString(R.string.gcm_send_message));
} else {
Log.i(TAG, getString(R.string.token_error_message));
}
}
};
// Registering broadcast receiver
registerReceiver();
if (checkPlayServices()) {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
/*----------------------------------------------------------------------------------------*/
/*---------------------------------AsyncTask that makes the ListView ---------------------*/
/*----------------------------------------------------------------------------------------*/
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ChannelList.this, this);
taskRead.execute(SERVER_ADDRESS + "Script.php");
BindDictionary<Channel> dict = new BindDictionary<Channel>();
dict.addStringField(R.id.tvName, new StringExtractor<Channel>() {
@Override
public String getStringValue(Channel channel, int position) {
return channel.channel_name;
}
});
FunDapter<Channel> adapter = new FunDapter<>(
ChannelList.this, channelList, R.layout.layout_list, dict);
lvChannel = (ListView) findViewById(R.id.lvChannels);
lvChannel.setAdapter(adapter);
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
}
Then I have the GcmListenerService class: 然后,我有GcmListenerService类:
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String commercial = data.getString("commercial");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
Log.d(TAG, "Commercial: " + commercial);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
sendNotification(message);
// [END_EXCLUDE]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received GCM message.
*
* @param message GCM message received.
*/
private void sendNotification(String message) {
Intent intent = new Intent(this, ChannelList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, // 0 => Request code
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("GCM Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build()); // 0 => ID of notification
}
}
Then I have the RegistrationIntentService class: 然后,我有了RegistrationIntentService类:
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
private static final String[] TOPICS = {"global"};
public static final String SENT_TOKEN_TO_SERVER = "sentTokenToServer";
public static final String REGISTRATION_COMPLETE = "registrationComplete";
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// [START register_for_gcm]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
// See https://developers.google.com/cloud-messaging/android/start for details on this file.
// [START get_token]
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// [END get_token]
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
// Subscribe to topic channels
subscribeTopics(token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, true).apply();
// [END register_for_gcm]
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
/**
* Persist registration to third-party servers.
*
* Modify this method to associate the user's GCM registration token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
/**
* Subscribe to any GCM topics of interest, as defined by the TOPICS constant.
*
* @param token GCM token
* @throws IOException if unable to reach the GCM PubSub service
*/
// [START subscribe_topics]
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
// [END subscribe_topics]
}
The InstanceIDListenerService: InstanceIDListenerService:
public class MyInstanceIDListenerService extends InstanceIDListenerService {
private static final String TAG = "MyInstanceIDLS";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
// [END refresh_token]
}
And the AndroidManifest: 和AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tumex.useralpha"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-permission android:name="android.permission.INTERNET"/>
<!-- [START gcm_permission] -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- [END gcm_permission] -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="@android:icon"
>
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChannelList"></activity>
<!-- [START gcm_receiver] -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="gcm.play.android.samples.com.gcmquickstart" />
</intent-filter>
</receiver>
<!-- [END gcm_receiver] -->
<!-- [START gcm_listener] -->
<service
android:name="com.tumex.useralpha.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<!-- [END gcm_listener] -->
<!-- [START instanceId_listener] -->
<service
android:name="com.tumex.useralpha.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<!-- [END instanceId_listener] -->
<service
android:name="com.tumex.useralpha.RegistrationIntentService"
android:exported="false">
</service>
</application>
So how do I go about this, make a ImageView react to values received from gcm message? 那么我该如何解决这个问题,让ImageView对从gcm消息收到的值做出反应? If I have forgotten anything that can be helpful in solving my problem please let me know.
如果我忘记了任何有助于解决我的问题的事情,请告诉我。 Thank you
谢谢
I suppose you want to update ImageView
in ListView
only if Activity with this ListView
is showing. 我想只有在显示带有此
ListView
Activity时才想在ListView
更新ImageView
。
In that case use LocalBroadcastManager : 在这种情况下,请使用LocalBroadcastManager :
YourActivity.java YourActivity.java
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//int for List, String for Dictionary
String item_id_or_name = intent.getExtra("ITEM_ID_OR_NAME");
int status_for_item = intent.getExtra("ITEM_STATUS");
//find this item in you List or Dictionary
//and set new Status for this item
yourAdapter.notifyDataSetChanged();
}
};
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(YourActivity.this).registerReceiver(receiver,
new IntentFilter("FILTER_NAME"));
}
@Override
public void onPause() {
LocalBroadcastManager.getInstance(YourActivity.this).unregisterReceiver(receiver);
super.onPause();
}
YourGcmListenerService.java : YourGcmListenerService.java :
@Override
public void onMessageReceived(String from, Bundle data) {
//...parse received data
Intent updateStatus = new Intent("FILTER_NAME");
updateStatus.putExtra("ITEM_ID_OR_NAME", id_or_name);
updateStatus.putExtra("ITEM_STATUS", item_status);//-1, 0, 1
LocalBroadcastManager.getInstance(this).sendBroadcast(updateStatus);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.