androig-gcm-client
androig-gcm-client
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.hmkcode.android.gcm;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.content.WakefulBroadcastReceiver;
|
||||
|
||||
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
// Explicitly specify that GcmMessageHandler will handle the intent.
|
||||
ComponentName comp = new ComponentName(context.getPackageName(),
|
||||
GcmMessageHandler.class.getName());
|
||||
|
||||
// Start the service, keeping the device awake while it is launching.
|
||||
startWakefulService(context, (intent.setComponent(comp)));
|
||||
setResultCode(Activity.RESULT_OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.hmkcode.android.gcm;
|
||||
|
||||
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class GcmMessageHandler extends IntentService {
|
||||
|
||||
String mes;
|
||||
private Handler handler;
|
||||
public GcmMessageHandler() {
|
||||
super("GcmMessageHandler");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
// TODO Auto-generated method stub
|
||||
super.onCreate();
|
||||
handler = new Handler();
|
||||
}
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
Bundle extras = intent.getExtras();
|
||||
|
||||
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
|
||||
// The getMessageType() intent parameter must be the intent you received
|
||||
// in your BroadcastReceiver.
|
||||
String messageType = gcm.getMessageType(intent);
|
||||
|
||||
mes = extras.getString("title");
|
||||
showToast();
|
||||
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
|
||||
|
||||
|
||||
|
||||
GcmBroadcastReceiver.completeWakefulIntent(intent);
|
||||
|
||||
}
|
||||
|
||||
public void showToast(){
|
||||
handler.post(new Runnable() {
|
||||
public void run() {
|
||||
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.hmkcode.android.gcm;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.app.Activity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class MainActivity extends Activity implements OnClickListener {
|
||||
|
||||
Button btnRegId;
|
||||
EditText etRegId;
|
||||
GoogleCloudMessaging gcm;
|
||||
String regid;
|
||||
String PROJECT_NUMBER = "1024888653441";
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
btnRegId = (Button) findViewById(R.id.btnGetRegId);
|
||||
etRegId = (EditText) findViewById(R.id.etRegId);
|
||||
|
||||
btnRegId.setOnClickListener(this);
|
||||
}
|
||||
public void getRegId(){
|
||||
new AsyncTask<Void, Void, String>() {
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
String msg = "";
|
||||
try {
|
||||
if (gcm == null) {
|
||||
gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
|
||||
}
|
||||
regid = gcm.register(PROJECT_NUMBER);
|
||||
msg = "Device registered, registration ID=" + regid;
|
||||
Log.i("GCM", msg);
|
||||
|
||||
|
||||
} catch (IOException ex) {
|
||||
msg = "Error :" + ex.getMessage();
|
||||
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String msg) {
|
||||
etRegId.setText(msg + "\n");
|
||||
}
|
||||
}.execute(null, null, null);
|
||||
}
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
getRegId();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user