Created
October 28, 2015 15:38
-
-
Save fnishio/91fba5f283945c2d8a7f to your computer and use it in GitHub Desktop.
Android Advertising ID を取得する ref: http://qiita.com/f_nishio/items/e3665a93b6e061f9ea0f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.gr.java_conf.fofn.advertisingid; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.TextView; | |
import com.google.android.gms.ads.identifier.AdvertisingIdClient; | |
import com.google.android.gms.common.GooglePlayServicesNotAvailableException; | |
import com.google.android.gms.common.GooglePlayServicesRepairableException; | |
import java.io.IOException; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "AdvertisingId"; | |
class AdIdTask extends AsyncTask<Void, Void, String> { | |
private Activity mActivity; | |
AdIdTask(Activity activity) { | |
mActivity = activity; | |
} | |
@Override | |
protected String doInBackground(Void... params) { | |
String advertisingId = ""; | |
try { | |
AdvertisingIdClient.Info info = | |
AdvertisingIdClient.getAdvertisingIdInfo(mActivity.getApplicationContext()); | |
advertisingId = info.getId(); | |
} catch (GooglePlayServicesNotAvailableException e) { | |
Log.e(TAG, "GooglePlayServices not available."); | |
} catch (GooglePlayServicesRepairableException e) { | |
// | |
} catch (IOException e) { | |
// | |
} | |
return advertisingId; | |
} | |
@Override | |
protected void onPostExecute(String id) { | |
TextView view = (TextView)mActivity.findViewById(R.id.adid); | |
view.append(id); | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// AdvertisingIdClient cannot be called in the main thread. | |
AsyncTask<Void, Void, String> task = new AdIdTask(this); | |
task.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment