我是Android開發人員開發應用程序。在這裏我從GCM獲取註冊ID並將其保存在一個字符串中,現在我想從廣播接收器發送此註冊ID類到我的mainActivity類,以便我可以將這個值發送到我的服務器。我試圖使用intent但強制關閉始終發生。對於GcmBroadcastreceiver.class我如何發送註冊ID從GCM在broadcastReceiver獲取主要活動
代碼
公共無效的onReceive(上下文語境,意圖意圖) {
String action=intent.getAction();
Log.d("msg", action);
if(action.equals("com.google.android.c2dm.intent.REGISTRATION"))
{
Log.d("msg", "within if loop");
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
Log.d("msg","Dumping Intent start");
while (it.hasNext()) {
String key = it.next();
// Log.d("msg","[" + key + "=" + bundle.get(key)+"]");
Toast.makeText(context,("msg"+"[" + key + "=" + bundle.get(key)+"]"), Toast.LENGTH_LONG).show();
}
Log.d("msg","Dumping Intent end");
}
String registrationId=intent.getStringExtra("registration_id");
Toast.makeText(context, "reg is"+registrationId, Toast.LENGTH_LONG).show();
Intent in=new Intent(context,MainActivity.class);
in.putExtra("RegID", registrationId);
context.startActivity(in);
Toast.makeText(context, "Intent is send to mainactivity class", Toast.LENGTH_LONG).show();
// Log.d("regid", registrationId);
String error=intent.getStringExtra("error");
//Log.d("error", error);
String unregistered=intent.getStringExtra("unregistered");
// Log.d("unregistered", unregistered);
}
else if(action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
String data1=intent.getStringExtra("data1");
Log.d("data1::",data1);
String data2=intent.getStringExtra("data2");
Log.d("data2::",data2);
}
請發佈您的logcat –