parent
ae2ac49e36
commit
c12512cf9c
@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.hmkcode.android.gcm"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0" >
|
||||||
|
|
||||||
|
<uses-sdk
|
||||||
|
android:minSdkVersion="8"
|
||||||
|
android:targetSdkVersion="17" />
|
||||||
|
|
||||||
|
<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.hmkcode.android.gcm.permission.C2D_MESSAGE"
|
||||||
|
android:protectionLevel="signature" />
|
||||||
|
<uses-permission android:name="com.hmkcode.android.gcm.permission.C2D_MESSAGE" />
|
||||||
|
|
||||||
|
<application
|
||||||
|
|
||||||
|
android:allowBackup="true"
|
||||||
|
android:icon="@drawable/ic_launcher"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:theme="@style/AppTheme"
|
||||||
|
>
|
||||||
|
<activity
|
||||||
|
android:name="com.hmkcode.android.gcm.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="com.hmkcode.android.gcm.GcmBroadcastReceiver"
|
||||||
|
android:permission="com.google.android.c2dm.permission.SEND" >
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
|
||||||
|
<category android:name="com.hmkcode.android.gcm" />
|
||||||
|
</intent-filter>
|
||||||
|
</receiver>
|
||||||
|
<service android:name="com.hmkcode.android.gcm.GcmMessageHandler" />
|
||||||
|
|
||||||
|
<meta-data android:name="com.google.android.gms.version"
|
||||||
|
android:value="@integer/google_play_services_version" />
|
||||||
|
</application>
|
||||||
|
|
||||||
|
</manifest>
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
@ -0,0 +1,19 @@
|
|||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".MainActivity"
|
||||||
|
android:orientation="vertical" >
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnGetRegId"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Get RegId"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/etRegId"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="250dp" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_settings"
|
||||||
|
android:orderInCategory="100"
|
||||||
|
android:showAsAction="never"
|
||||||
|
android:title="@string/action_settings"/>
|
||||||
|
|
||||||
|
</menu>
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
<resources>
|
||||||
|
|
||||||
|
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||||
|
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||||
|
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
|
||||||
|
<string name="app_name">Android GCM</string>
|
||||||
|
<string name="action_settings">Settings</string>
|
||||||
|
<string name="hello_world">Hello world!</string>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
<resources>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Base application theme, dependent on API level. This theme is replaced
|
||||||
|
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||||
|
-->
|
||||||
|
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||||
|
<!--
|
||||||
|
Theme customizations available in newer API levels can go in
|
||||||
|
res/values-vXX/styles.xml, while customizations related to
|
||||||
|
backward-compatibility can go here.
|
||||||
|
-->
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<!-- Application theme. -->
|
||||||
|
<style name="AppTheme" parent="AppBaseTheme">
|
||||||
|
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||||
|
</style>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue