gpt4 book ai didi

android - 收到 C2DM 推送通知时更改 Activity 行为

转载 作者:搜寻专家 更新时间:2023-11-01 09:14:34 25 4
gpt4 key购买 nike

我有一个可以向我的设备发送推送通知的工作服务器/客户端解决方案。我的项目的下一步是在 C2DMReceiver 类中调用 onReceive 事件时在 Activity 窗口上显示一个对话框。

由于我是 android 的新手,我不知道该怎么做,所以如果有人能向我解释一下我会很高兴。

基本上,我为 c2dm 重用了 chrometophone 应用程序中的类。 onReceive 事件被调用,因为我为 logcat 创建了一个日志条目。由于 C2DMReceiver 是一项服务,如果有新消息,我如何获得有关我的 Activity 的通知?

我在谷歌上搜索了很多但找不到可行的解决方案...我尝试使用 registerReceiver() 但我很确定我做错了。有没有人有例子?

好的,这是我到目前为止得到的:

Activity

    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);
}

C2DM 接收器公共(public)类 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);

}

}

这就是我得到的全部内容,但我从来没有收到自己的广播。有人有意见吗?

最佳答案

这应该可以做到。

@Override
protected void onMessage(Context context, Intent intent) {

Log.w(Consts.LOG_TAG_SERVICE, "C2DMReceiver: " + intent.getStringExtra("payload"));

Intent i = new Intent(context, YourMainActivity.class);
i.putExtra("reading", intent.getStringExtra("payload"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

}

然后您在主 Activity 的 onStart 中处理 Intent 。如果您的 Activity 已经在运行,它将由现有实例处理,如果没有,将启动一个新实例。

关于android - 收到 C2DM 推送通知时更改 Activity 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5662626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com