无法在 Android 中注销 GCM

Unable to unregister GCM in Android

我正在尝试在用户注销时注销 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.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)

文档中提到

所以我也尝试用这个

 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.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();
}

试试这个来注销 GCM。 GCMRegistrar.unregister(getApplicationContext());