将接收器数据发送到 Activity
Send Receiver data to Activity
我想分享 GCM 从 Receiver 接收数据到 Activity。如果Activity是active/onResume/onPause状态,我想把数据显示给activity。如果 activity 被破坏,那么我想在通知栏中显示该信息。
接收器 -> GcmMessageHandler.class
import android.os.Bundle;
import com.google.android.gms.gcm.GcmListenerService;
public class GcmMessageHandler extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
createNotification(message);
}
private void createNotification(String body) {
String sendDataToActivity = body; // This is value i want to pass to activity
}
}
Activity -> MainActivity.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
String RECEIVER_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setText(RECEIVER_VALUE); // Here i want to set the receiver valiue
}
}
这里是通知功能。
private void Notify(String notificationMessage){
Notification notification = new Notification.Builder(getBaseContext())
.setContentText(notificationMessage) // Show the Message Here
.setSmallIcon(R.drawable.ic_menu_gallery)
.setWhen(System.currentTimeMillis())
.build();
notification.notify();
}
我已经在 Activity 中使用了从 onNewIntent 方法接收的意图。
来自接收方 ->
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("message",message);
getApplicationContext().startActivity(intent);
从 Activity ->
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String message = intent.getExtras().getString("message").toString();
textView.setText(message);
}
但问题是,如果我的 activity 关闭,它会重新打开 activity。除了这个还有其他解决方案吗?
您可以像这样使用 EventBus:
在你的申请中class
@Override
public void onCreate() {
super.onCreate();
EventBus.builder()
.logNoSubscriberMessages(false)
.sendNoSubscriberEvent(false)
.throwSubscriberException(false)
.installDefaultEventBus();
}
在您的 Activity 上添加此方法,每次您通过 EventBus 创建 post 时都会调用此方法。
@Subscribe
public void onEventFromReceiver(ReceiverEvent event){
Log.d(TAG, event.message);
}
关于你的"send data to Activity"方法
EventBus.getDefault().post(new ReceiverEvent(message));
还有你的 ReceiverEvent class
public class ReceiverEvent {
public final String message;
public ReceiverEvent(String message) {
this.message = message;
}
}
这样,如果 activity 不可见或应用程序在后台,则不会发生任何事情。
这是EventBus项目
希望对您有所帮助。
我想分享 GCM 从 Receiver 接收数据到 Activity。如果Activity是active/onResume/onPause状态,我想把数据显示给activity。如果 activity 被破坏,那么我想在通知栏中显示该信息。
接收器 -> GcmMessageHandler.class
import android.os.Bundle;
import com.google.android.gms.gcm.GcmListenerService;
public class GcmMessageHandler extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
createNotification(message);
}
private void createNotification(String body) {
String sendDataToActivity = body; // This is value i want to pass to activity
}
}
Activity -> MainActivity.class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
String RECEIVER_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
textView.setText(RECEIVER_VALUE); // Here i want to set the receiver valiue
}
}
这里是通知功能。
private void Notify(String notificationMessage){
Notification notification = new Notification.Builder(getBaseContext())
.setContentText(notificationMessage) // Show the Message Here
.setSmallIcon(R.drawable.ic_menu_gallery)
.setWhen(System.currentTimeMillis())
.build();
notification.notify();
}
我已经在 Activity 中使用了从 onNewIntent 方法接收的意图。
来自接收方 ->
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("message",message);
getApplicationContext().startActivity(intent);
从 Activity ->
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String message = intent.getExtras().getString("message").toString();
textView.setText(message);
}
但问题是,如果我的 activity 关闭,它会重新打开 activity。除了这个还有其他解决方案吗?
您可以像这样使用 EventBus:
在你的申请中class
@Override
public void onCreate() {
super.onCreate();
EventBus.builder()
.logNoSubscriberMessages(false)
.sendNoSubscriberEvent(false)
.throwSubscriberException(false)
.installDefaultEventBus();
}
在您的 Activity 上添加此方法,每次您通过 EventBus 创建 post 时都会调用此方法。
@Subscribe
public void onEventFromReceiver(ReceiverEvent event){
Log.d(TAG, event.message);
}
关于你的"send data to Activity"方法
EventBus.getDefault().post(new ReceiverEvent(message));
还有你的 ReceiverEvent class
public class ReceiverEvent {
public final String message;
public ReceiverEvent(String message) {
this.message = message;
}
}
这样,如果 activity 不可见或应用程序在后台,则不会发生任何事情。
这是EventBus项目
希望对您有所帮助。