gpt4 book ai didi

android - 无法在 Android 中注销 GCM

转载 作者:行者123 更新时间:2023-11-29 20:36:33 26 4
gpt4 key购买 nike

我正在尝试在用户注销时取消注册 GCM。我正在使用以下代码来执行此操作。

public void logout(){

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(NewSettings.this);
try {
gcm.unregister();
} catch (IOException e) {
e.printStackTrace();
e.toString();
Toast.makeText(NewSettings.this,"Problem in logout, try again later!",Toast.LENGTH_LONG).show();
}
}

这不会注销 GCM,因为我在 logcat 中遇到了这样的异常

07-14 10:54:20.718  26537-26537/com.artqueen.aahaan W/System.err﹕ java.io.IOException: MAIN_THREAD
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at com.google.android.gms.gcm.GoogleCloudMessaging.unregister(Unknown Source)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at com.artqueen.aahaan.Activities.NewSettings.logout(NewSettings.java:157)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at com.artqueen.aahaan.Activities.NewSettings$7.onClick(NewSettings.java:120)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.view.View.performClick(View.java:5254)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.view.View$PerformClick.run(View.java:21173)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:6837)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
07-14 10:54:20.718 26537-26537/com.artqueen.aahaan W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

文档提到

documentation

所以我也尝试用这个

 public void logout(){

InstanceID instanceID = InstanceID.getInstance(NewSettings.this);
try {
instanceID.deleteInstanceID();
} catch (IOException e) {
e.printStackTrace();
e.toString();
Toast.makeText(NewSettings.this,"Problem in logout, try again later!",Toast.LENGTH_LONG).show();
}

我仍然在 logcat

中得到这个
07-14 11:02:03.228  31682-31682/com.artqueen.aahaan W/System.err﹕ java.io.IOException: MAIN_THREAD
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.google.android.gms.iid.InstanceID.zzb(Unknown Source)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.google.android.gms.iid.InstanceID.deleteInstanceID(Unknown Source)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.artqueen.aahaan.Activities.NewSettings.logout(NewSettings.java:158)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.artqueen.aahaan.Activities.NewSettings$7.onClick(NewSettings.java:121)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.view.View.performClick(View.java:5254)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.view.View$PerformClick.run(View.java:21173)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:6837)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
07-14 11:02:03.238 31682-31682/com.artqueen.aahaan W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

我是否以正确的方式注销了 GCM?请让我知道如何实现它。提前致谢。

最佳答案

我需要在新线程中运行它。这解决了我的问题。谢谢@calvinfly。

public void logout(){

instanceID = InstanceID.getInstance(NewSettings.this);

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
instanceID.deleteInstanceID();
} catch (Exception bug) {
bug.printStackTrace();
}

}
});
thread.start();
}

关于android - 无法在 Android 中注销 GCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com