如何在GCM接收器中获取startActivityForResult

时间:2013-12-06 02:36:44

标签: java android android-intent broadcast google-cloud-messaging

请帮帮我。我需要从GCM接收器获取活动结果,代码在

下面

在MainActivity.class上

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
      //PARSE RESULT CODE
       if (resultCode == RESULT_OK){
           Toast.makeText(_context,"WAS LOGIN", Toast.LENGTH_LONG).show();
           mainProgram();
        }else if(resultCode == RESULT_CANCELED){
           Toast.makeText(_context,"WAS NOT LOGIN", Toast.LENGTH_LONG).show();
           showLoginForm();
        }
  }


@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(SampleList.THEME); // Used for theme switching in samples
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(MainActivity.this, LoginProc.class);
    startActivityForResult(intent, 0);      
}
private void mainProgram(){
 setContentView(R.layout.mainlayout);
}

并在LoginProc.class中

@Override
protected void onCreate(Bundle savedInstanceState) {    
     GCMRegistrar.checkDevice(this);
     GCMRegistrar.checkManifest(this);  
     GCMRegistrar.register(this, SENDER_ID);
     registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION));
     this.regId = GCMRegistrar.getRegistrationId(this);
     GCMRegistrar.register(this, SENDER_ID);
}

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());
        Toast.makeText(getApplicationContext(),"New Message On MHandled:"+newMessage, Toast.LENGTH_LONG).show();

        // Releasing wake lock
        WakeLocker.release();

        //hide progress
        showProgress(false); 

        //set result vars for this intent
        Intent i=new Intent();
    LoginProc.this.setResult(RESULT_OK,i);
        //Intent i = new Intent();
    //setResult(RESULT_CANCELED, i);
    finish();
    }
};

并且结果总是显示ToastText“WAS NOT LOGIN”,即使事件GCM成功后(Toas“新消息MHandled”显示器)。

在这里需要正确的方向,非常感谢

1 个答案:

答案 0 :(得分:0)

确保您已获得所有必需的权限,并通过GCMIntentService接收消息,该消息必须在Manifest中注册。

检查此 link

这将告诉GCM注册和从服务器获取推送消息的步骤。