1
GCM과 함께 작업하고 있지만 알림을 받았을 때 내 onResume()
에서 내 BroadcastReceiver
을 등록하는 중 하나의 문제에 직면하고 있습니다. 그러나 때때로 onResume()
함수가 2 번 호출하기 때문에이 문제가 발생합니다. 문제. 나는 왜 이것이 2 번 일하고 있는지 이해하지 못하고있다.안드로이드에서 2 번 호출 기능.
코드 GCM 의도 서비스 클래스 : 활동의
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(this, msgg);
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(this, msgg);
updateMyActivity(this, msgg);
bundle.putString("result", msgg);
receiver.send(STATUS_FINISHED, bundle);
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
updateMyActivity(this,msgg);
sendNotification(this, msgg);
}
}
// GcmBroadcastReceiver.completeWakefulIntent(intent);
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
//This function will create an intent. This intent must take as parameter the "unique_name" that you registered your activity with
static void updateMyActivity(Context context, String message) {
Log.e(TAG, "updateMyActivity: ");
Intent intent = new Intent("unique_name");
//put whatever data you want to send, if any
intent.putExtra("message", message);
//send broadcast
context.sendBroadcast(intent);
}
onResume()
방법 :
@Override
public void onStart()
{
super.onStart();
onResume();
}
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(
"unique_name");
mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//extract our message from intent
String msg_for_me = intent.getStringExtra("message");
}
하지만이 "msg_for_me"2 번 데. 스크린 샷을 확인하십시오. http://prntscr.com/dpzxrs
왜 ONSTART에 onResume 부릅니까? – 2ndGAB
내가 그때 내 이력서 fucntion은 당신이 뜻하는 일 2ndGAB –
@ 시간 동안 모든 시간 일을 작동하지 호출하고 있지 않다 경우 becuase는 일을하지 않거나 호출하지의 '작동하지'. 다른 것은 복사/붙여 넣기 실수입니까, 아니면 방송 수신기가 onResume()에 있습니까 ?? 당신은 onCreate()와 registerReceiver() 호출과 unRegisterReceiver()를 Destroy()에 더 잘 넣어야한다. – 2ndGAB