Google Cloud Messaging - 检查设备是否已经注册 [英] Google Cloud Messaging - Check if device is already registered
本文介绍了Google Cloud Messaging - 检查设备是否已经注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只在我的Android应用中设置GCM。但我有问题,我不知道如何检查设备是否已经注册。我使用新的谷歌播放服务库。
I just set up GCM in my Android App. But I have the problem that I don't know how to check if the device is already registered. I work with the new google play services library.
注册部分如下所示:
The register part looks like this:
@Override
protected String doInBackground(String... arg0) {
String msg = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context_app);
}
regid = gcm.register(SENDER_ID);
msg = "Dvice registered, registration ID=" + regid;
Log.d("111", msg);
sendRegistrationIdToBackend(regid);
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
如何修改它以检查设备是否已经注册? / p>
How can I modify this that it checks if the device is already registered?
推荐答案
当您获得注册ID时,请将其存储在SharedPreferences中,例如:
when you get registration Id, Store it in SharedPreferences, for example:
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",MODE_PRIVATE);
SharedPreferences.Editor editor=shp.edit();
editor.putString("RegID",registrationID).commit;
在下一次注册之前,请检查anyNameYouLike是否包含名为RegID的字段。像这样:
In the next time before you register check the "anyNameYouLike" if it contain field called RegID Like this:
private boolean isRegistered(Context context){
SharedPreferences shp = context.getSharedPreferences("anyNameYouLike",PRIVATE_MODE);
return shp.contains("RegID");
}
这篇关于Google Cloud Messaging - 检查设备是否已经注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文