Unity干货Gradle

[Unity]安卓手机接入google play service

2017-02-23  本文已影响1247人  阿飞咯

必须条件:

1.手机翻墙:直接百度最为方便
2.开启google service:建议直接下载Play商店,首先就会提示你开启谷歌服务

正式接入:参考
主要是用jar接入,现在google play service现在不提供jar更新下来都是aar,所以我们需要提取jar

sdk\extras\google\m2repository\com\google\android\gms\play-services{version}
将aar用压缩包打开,然后提取classes.jar 就是我们需要的jar
最后配置AndroidManifest

    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

google_play_services_version = 版本号
比如:
version = 6.5.87
google_play_services_version = 658700
测试代码:

package tech.xiefei.test;

import java.io.IOException;

import com.example.googleplaytest.R;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity{
    TextView versionText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        versionText = (TextView) findViewById(R.id.version);
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                getIdThread();
            }
        }).start();
    }
    Handler handler = new Handler();
    // Do not call this function from the main thread. Otherwise, 
    // an IllegalStateException will be thrown.
    public void getIdThread() {

      Info adInfo = null;
      try {
        adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());

      } catch (IOException e) {
        // Unrecoverable error connecting to Google Play services (e.g.,
        // the old version of the service doesn't support getting AdvertisingId).
          e.printStackTrace();
          return;
      } catch (GooglePlayServicesNotAvailableException e) {
        // Google Play services is not available entirely.
          e.printStackTrace();
          return;
      } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    } catch (GooglePlayServicesRepairableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return;
    }
      final String id = adInfo.getId();
      final boolean isLAT = adInfo.isLimitAdTrackingEnabled();
      handler.post(new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            versionText.setText(id);
        }
    }); 
    }

}

上一篇 下一篇

猜你喜欢

热点阅读