2014年1月21日火曜日

Google Play ServicesでGCMを使ってみる(Google Play Services APKの存在チェック編)

詰まったところをピンポイントで覚え書き。
GCMを使おうとSDK Managerで「Google Cloud Messaging for Android Library」をインストール。。。
Derecatedになってるぞ!
GCMのオフィシャルサイトで確認してみると、「Google Play Services」を使いなさいとのこと。
いつの間に変わったのよ!?って、Google I/O 2013の時ですよね。そいえば中継見てましたよ。時間的にめっちゃ眠かった(笑)
そんなこんなでオフィシャルサイトの「Implementing GCM Client」ページを見ながら環境作りました。書いてあるまんま! とりあえずPlay ServicesのAPKがあるかどうかをチェックする必要があるらしいので、そこだけ書いて動かしてみよう。
ManifestにパーミッションとReceiverを追加しておけばよし。ここはGoogle Play Servicesに変わる前と一緒みたい。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.helloworld.app" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.helloworld.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.helloworld.app.permission.C2D_MESSAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:theme="@style/Theme.AppCompat"
            android:name="com.helloworld.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.helloworld.app" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmIntentService" />
    </application>
</manifest>
で、チェック用のメソッドがこれ。
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Log.i(TAG, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}

それと、忘れちゃいけないライブラリの追加。Android Studioを使っているのでGradleにお任せ。
これはオフィシャルサイトの通りではライブラリのバージョンが古いので、「3.1.+」から現行バージョンの「4.1.+」に変更し、変なところに「:」があるので「compile」直後の「:」は外しておきます。
その前に、googleのrepositoryを見に行くので、SDK Managerでrepositoryをインストールしておきます。
(キャプチャでは先にインストールしてしまったので「Installed」になっています)
build.gradle設定を実施し、ツールバーの真ん中あたりにある「Sync Project with Gradle Files」をポチッとします。
dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:4.1.+'
}
はい、これでビルドもして実機転送!

あれ?またこないだ見たやつ。(一緒だったからキャプチャは撮り直してません)
落ち着いてlogcatでエラーを確認してみましょ。
01-21 22:57:44.687  14820-14820/com.helloworld.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.helloworld.app/com.helloworld.app.MainActivity}: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the  element:     
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
            at android.app.ActivityThread.access$600(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1215)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4798)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the  element:     
            at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
            at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
            at com.helloworld.app.MainActivity.checkPlayServices(MainActivity.java:78)
            at com.helloworld.app.MainActivity.onCreate(MainActivity.java:31)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2043)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
            at android.app.ActivityThread.access$600(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1215)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4798)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
2行目にmetaタグねーよ!と書かれています。
ここは素直に指示に従います。
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:theme="@style/Theme.AppCompat"
            android:name="com.helloworld.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
はい、これで無事動きました。


0 件のコメント :

コメントを投稿