我有一个可以向我的设备发送推送通知的工作服务器/客户端解决方案。我的项目的下一步是在C2DMReceiver
类中调用 onReceive 事件时在活动窗口上显示一个对话框。
由于我是 android 新手,我不知道如何做到这一点,所以如果有人可以向我解释这一点,我会非常高兴。
基本上我重用了 chrometophone Application for c2dm 中的类。当我为 logcat 创建一个日志条目时,将调用 onReceive 事件。作为C2DMReceiver
一项服务,如果有新消息,我如何获悉我的活动?
我用谷歌搜索了很多,但找不到有效的解决方案……我尝试使用,registerReceiver()
但我很确定我做错了。有人有例子吗?
好的,这就是我到目前为止得到的:
活动
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "Test");
}
};
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w(Consts.LOG_TAG_SERVICE, "started");
}
// Called when the activity is restarted
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addCategory("com.mydomain.myapp.CONTEXT");
registerReceiver(mReceiver, filter);
}
// Called when the activity is closed
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
C2DMReceiver 公共类 C2DMReceiver 扩展 C2DMBaseReceiver {
public C2DMReceiver() {
super("my_test@gmail.com");
// TODO Load dynamic Gmail address
}
@Override
public void onRegistrered(Context context, String registrationId) {
Log.i(Consts.LOG_TAG_SERVICE, registrationId);
// Store the registration id in the preferences
SharedPreferences settings = Prefs.get(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString("deviceRegistrationID", registrationId);
editor.commit();
// TODO: Send ID to server
}
@Override
public void onUnregistered(Context context) {
Log.w(Consts.LOG_TAG_SERVICE, "got here!");
}
@Override
public void onError(Context context, String errorId) {
Log.w(Consts.LOG_TAG_SERVICE, errorId);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.mydomain.myapp.NEWMESSAGE");
broadcastIntent.putExtra("reading", intent.getStringExtra("payload"));
broadcastIntent.addCategory("com.mydomain.myapp.CONTEXT");
context.sendBroadcast(broadcastIntent);
}
}
这就是我所得到的,但我从来没有收到我自己的广播。有没有人有一些意见?