Created
June 20, 2011 22:52
-
-
Save takimo/1036793 to your computer and use it in GitHub Desktop.
C2DMサンプル
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
/** | |
* net.takimo.tools.sharepageとなっているところを自分のパッケージに変更(GoogleのC2DMサービスに登録したパッケージでないといけない) | |
*/ | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="net.takimo.tools.sharepage" | |
android:versionCode="1" | |
android:versionName="1.0"> | |
<uses-sdk android:minSdkVersion="7" /> | |
<permission android:name="net.takimo.tools.sharepage.permission.C2D_MESSAGE" android:protectionLevel="signature" /> | |
<uses-permission android:name="net.takimo.tools.sharepage.permission.C2D_MESSAGE" /> | |
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<application android:label="@string/app_name" | |
android:icon="@drawable/icon"> | |
<activity android:name="SharePage" | |
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=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> | |
<intent-filter> | |
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> | |
<category android:name="net.takimo.tools.sharepage" /> | |
</intent-filter> | |
<intent-filter> | |
<action android:name="com.google.android.c2dm.intent.REGISTRATION" /> | |
<category android:name="net.takimo.tools.sharepage" /> | |
</intent-filter> | |
</receiver> | |
</application> | |
</manifest> |
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 net.takimo.tools.sharepage; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Message; | |
import android.util.Log; | |
import com.google.android.c2dm.C2DMBaseReceiver; | |
public class C2DMReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d("c2dm","onReceive"); | |
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { | |
handleRegistration(context, intent); | |
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { | |
String str = intent.getStringExtra("message"); | |
sendMessage(str); | |
} | |
} | |
private void handleRegistration(Context context, Intent intent) { | |
String registration = intent.getStringExtra("registration_id"); | |
if (intent.getStringExtra("error") != null) { | |
Log.d("c2dm","error"); | |
} else if (intent.getStringExtra("unregistered") != null) { | |
Log.d("c2dm","unregistered"); | |
} else if (registration != null) { | |
Log.d("c2dm",registration); | |
} | |
} | |
private void sendMessage(String str) { | |
Message mes = Message.obtain(SharePage.mH); | |
Bundle data = mes.getData(); | |
data.putBoolean("receivedMessageFlag", true); | |
data.putString("receivedMessageString", str); | |
SharePage.mH.sendMessage(mes); | |
mes = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment