GCMRegistrar.checkDevice(this);
GCMRegistrar.unregister(this);
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
GCMRegistrar.getRegistrationId(this));
}
regId = GCMRegistrar.getRegistrationId(this);
l.setText(regId);
if (regId.equals("")) {
GCMRegistrar.register(this, "187805647345");
GCMRegistrar.getRegistrationId(this));
l.setText(regId);
}
else {
GCMRegistrar.getRegistrationId(this));
}
Toast.makeText(RegisterHalf.this,""+regId,2000).show();
在运行的应用程序,我得到设备ID = NULL
但是当我第二次运行设备ID
有的值。
When running the app I get device id = null
but when I run a second time device id
has a value.
请告诉我,为什么发生这种情况。
Please tell me why this is happening.
您必须从SDK导入谷歌播放服务项目导入到工作区,并添加 它作为一个lib到您的project.then遵循以下步骤:
you have to import google play services project from the sdk into your workspace and add it as a lib to your project.then follow the below steps:
1).//declare global
private GoogleCloudMessaging _gcm;
String _devideId;
2). then execute a new thread like this:
private class GetDeviceID extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
// progress bar
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
if (_gcm == null) {
_gcm = GoogleCloudMessaging.getInstance(CompanyRegistration.this);
}
try {
_devideId= _gcm.register("your project number of 12 digit");
System.out.println("my id is\n" + _devideId+ _devideId.length());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
//dismiss dialouge here
}
}
3). in _devideId String you get the device id.
4).modify your manifest file as
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.SEND" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
//在应用程序标签添加reciever
//in application tag add your reciever
<receiver
android:name="your package name.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="your package name" />
</intent-filter>
</receiver>
//广播reciever类
// broadcast reciever class
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
private SharedPreferences prefs;
Boolean alert = true;
@Override
public void onReceive(Context context, Intent intent) {
prefs = context.getSharedPreferences(
GcmBroadcastReceiver.class.getSimpleName(),
context.MODE_PRIVATE); // changes here
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
System.out.println("reciever is" + messageType);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
sendNotification("Deleted messages on server: "
+ intent.getExtras().toString());
} else {
Log.i("aaaaaa", intent.getExtras().toString());
sendNotification(intent.getExtras().getString("message").toString());
}
setResultCode(Activity.RESULT_OK);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, GcmBroadcastReceiver.class), 0); // changes here
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
ctx).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Come Fetch Me Notification ")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mBuilder.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
上一篇:删除图标但HomeAsUp在动作条图标、动作、HomeAsUp
下一篇:&QUOT;你需要使用Theme.Appcompat主题...&QUOT;测试ActionBarActivity的时候,但我但我、时候、测试、主题