3

アプリがエラーを出さないGCMサーバーから登録IDを取得しません。これは私のlogcatです。

    07-13 16:43:39.920: I/** pushAndroidActivity **(5310): inside oncreate() 
07-13 16:43:39.920: I/** pushAndroidActivity **(5310): started registration 
07-13 16:43:39.920: D/GCMRegistrar(5310): resetting backoff for com.example.registration_id
07-13 16:43:39.920: V/GCMRegistrar(5310): Registering app com.example.registration_id of senders 803641917196
07-13 16:43:42.735: V/GCMBroadcastReceiver(5310): onReceive: com.google.android.c2dm.intent.REGISTRATION
07-13 16:43:42.735: V/GCMBroadcastReceiver(5310): GCM IntentService class: com.example.registration_id.GCMIntentService
07-13 16:43:42.735: V/GCMBaseIntentService(5310): Acquiring wakelock

これはメインのアクティビティです。既存の gcm.jar ファイルを使用して GCMRegistrar クラス メソッドを使用しました。

package com.example.registration_id;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.app.Activity;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity {


    private String TAG = "** pushAndroidActivity **";
    EditText edittext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "inside oncreate() ");




        //check if registered,if not get registration id from GCM server
        if(GCMRegistrar.getRegistrationId(this).equals(""))
        {
            Log.i(TAG, "started registration ");
            GCMRegistrar.register(this, "803641917206");

        }
        else
        {
        final String regId = GCMRegistrar.getRegistrationId(this);
        Log.i(TAG, "registration id =====  "+regId);

        edittext=(EditText)findViewById(R.id.msg);
        edittext.setText(regId);
        }
    }




}

これはサービス クラスです。

package com.example.registration_id;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "===GCMIntentService===";



    //default constructor
    protected GCMIntentService(String senderId) {
        super(senderId);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub
        Log.i(TAG, "error ");
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Device registered: regId = " + registrationId);

    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

}
4

3 に答える 3

4
onReceive: com.google.android.c2dm.intent.REGISTRATION

onReceiver が呼び出されています。これは、メッセージが返されていることを意味します。

  protected GCMIntentService(String senderId) {
      super(senderId);                }  

これはあなたの問題です。GCMBaseIntentService サブクラスの引数なしの public コンストラクターを宣言する必要があります。そうしないと、GCMIntentService クラスを構築して、バックグラウンド サービスで適切に使用することができません。

なんで?

protected GCMIntentService(String senderID){
super(senderId);}

この保護されたコンストラクターは、別のパッケージにあるため、すべての着信インテントを処理するサービス クラスからは見えないためです。

GCMIntentService クラスの静的定数で sendId をハードコーディングします。これは、Google から変更されなくなるためです。

于 2012-07-13T12:11:22.127 に答える
1

これに注意してください: GCM IntentService クラス: com.example.registration_id.GCMIntentService

GCMIntentService クラスはどこに (どのパッケージに) 配置されていますか?

マニフェスト ファイルをチェックして、GCMIntentService の正しいパッケージを設定していることを確認してください。

于 2013-03-18T02:56:13.670 に答える