问题
I am new to android application development. I am developing an android application using c2dm. Can anyone tell me of how to receive the registration id for the application from c2dm. Please tell me with a detailed example and explanation
回答1:
public class C2dmEx extends Activity
{
static TextView mytext = null;
Context context = null;
Intent intent = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mytext = (TextView) findViewById(R.id.mytext);
mytext.setText("app started");
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender","your Mail Id");
startService(registrationIntent);
mytext.setText("Grabbing your device registration ID.....");
Log.i("Recieve","1");
}
}
public class C2DMReceiver extends BroadcastReceiver
{
//private CustomerBean customer = null;
private RegisterManager reg = null;
private C2dmEx ex = null;
int UserId = 0;
private boolean auth = false;
private CustLogin cl = null;
public C2DMReceiver()
{
cl = new CustLogin();
}
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("Recieve","2");
C2dmEx.mytext.setText("Intent Received!");
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
{
handleRegistration(context, intent);
}
else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE"))
{
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent)
{
String registration = intent.getStringExtra("registration_id");
if (intent.getStringExtra("error") != null)
{
C2dmEx.mytext.setText("There was an error with your device registration!");
// Registration failed, should try again later.
}
else if (intent.getStringExtra("unregistered") != null)
{
// unregistration done, new messages from the authorized sender will be rejected
C2dmEx.mytext.setText("You have been unregistered!");
}
else if (registration != null)
{
// Send the registration ID to the 3rd party site that is sending the messages.
// This should be done in a separate thread.
// When done, remember that all registration is done.
// UserId = customer.getId();
// Log.i("id",String.valueOf(UserId));
String RegId = registration;
Log.i("reg",String.valueOf(RegId) );
C2dmEx.mytext.setText("Your registration code is: " + RegId);
}
}
private void handleMessage(Context context, Intent intent)
{
C2dmEx.mytext.setText("You have been alerted!");
}
}
and your manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mmp.geopon"
android:versionCode="1"
android:versionName="0.1">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".C2dmEx"
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="com.mmp.geopon" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.mmp.geopon" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
<permission android:name="com.mmp.geopon.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.mmp.geopon.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.READ_PHONE_STATE"/>
</manifest>
来源:https://stackoverflow.com/questions/4924589/android-c2dm-programming-example