我正在开发一个应用程序。我正在使用此代码将我的注册ID从广播接收器类发送到主要活动类。
context.sendBroadcast(new Intent("com.example.sampletestproject.REGISTER_DEVICE"). putExtra("GCM_REG_ID",registrationId));
现在我想在Main Activity类中收到此注册ID。我试图通过创建一个新的广播接收器类但没有获得价值来做到这一点。 主要活动的代码是。
Button bt1,bt2;
BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver(mReceiver, new IntentFilter("com.example.sampletestproject.REGISTER_DEVICE"));
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
//Log.d("msg", "Button clicked");
Toast.makeText(MainActivity.this, "Button clicked", Toast.LENGTH_LONG).show();
Intent registrationIntent=new Intent("com.google.android.c2dm.intent.REGISTER");
// Log.d("msg", "Intent created");
Toast.makeText(MainActivity.this, "Intent created", Toast.LENGTH_LONG).show();
registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(),0,new Intent(),0));
registrationIntent.putExtra("senderid","908614649566");
startService(registrationIntent);
//Log.d("msg", "Intent Registered");
Toast.makeText(MainActivity.this, "Intent Register", Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent unregintent=new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregintent.putExtra("app", PendingIntent.getBroadcast(v.getContext(),0, new Intent(), 0));
startService(unregintent);
Toast.makeText(MainActivity.this, "Intent unregister", Toast.LENGTH_LONG).show();
}
});
mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//post registration id to server
Toast.makeText(MainActivity.this, "within new broadcast receiver in mainactivity", Toast.LENGTH_LONG).show();
Intent it=getIntent();
it.getExtras().get("GCM_REG_ID");
}
};
}
答案 0 :(得分:0)
应该是这样的。
按钮bt1,bt2;
BroadcastReceiver mReceiver; @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main);
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
//Log.d("msg", "Button clicked");
Toast.makeText(MainActivity.this, "Button clicked", Toast.LENGTH_LONG).show();
Intent registrationIntent=new Intent("com.google.android.c2dm.intent.REGISTER");
// Log.d("msg", "Intent created");
Toast.makeText(MainActivity.this, "Intent created", Toast.LENGTH_LONG).show();
registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(),0,new Intent(),0));
registrationIntent.putExtra("senderid","908614649566");
startService(registrationIntent);
//Log.d("msg", "Intent Registered");
Toast.makeText(MainActivity.this, "Intent Register", Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent unregintent=new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregintent.putExtra("app", PendingIntent.getBroadcast(v.getContext(),0, new Intent(), 0));
startService(unregintent);
Toast.makeText(MainActivity.this, "Intent unregister", Toast.LENGTH_LONG).show();
}
});
mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
//post registration id to server
Toast.makeText(MainActivity.this, "within new broadcast receiver in mainactivity", Toast.LENGTH_LONG).show();
Intent it=getIntent();
it.getExtras().get("GCM_REG_ID");
}
};registerReceiver(mReceiver, new IntentFilter("com.example.sampletestproject.REGISTER_DEVICE"));
}
答案 1 :(得分:0)
由于Receiver是在Activity中创建的,因此仅当活动开始时接收器才会处于活动状态。 Better Idea分别创建接收器,并在Android Manifest中注册 参考this example