Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:33:35+00:00 2026-05-31T06:33:35+00:00

I have got C2DM setup and working. It also sends the users ID to

  • 0

I have got C2DM setup and working. It also sends the users ID to my server so I can then send messages to it. However I am struggling updating the UI alerting the user with the status of their registration. The problem I have is that the registration is handled in a class which is not an activity, so I am struggling to figure out how to alert the user.

I have a class ‘RegistrationScreen’ which has the following:

import android.app.Activity;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RegistrationScreen extends Activity {


    public static int status;
    public static String userID;
    public static String regPass;
    public static int bankID=201;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registrationscreen);

        Button btn1 = (Button)findViewById(R.id.registerSubmitButton);
        btn1.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) {
                //Get data from form
                final EditText userIdText = (EditText) findViewById(R.id.userID);
                userID = userIdText.getText().toString();

                final EditText userPasswordText = (EditText) findViewById(R.id.userPassword);
                regPass = userPasswordText.getText().toString();


                register();
            }

        });
    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            Dialog superSimpleDlg = new Dialog(this);
            superSimpleDlg.setTitle("blah");
            return superSimpleDlg;

        }
        return null;
    }
    private void register() {
        status=1; //1= being processed
        String emailOfSender ="*removed*";
        Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
        registrationIntent.putExtra("sender", emailOfSender);
        startService(registrationIntent);

        //        while(status==1){
        //          
        //        }

        showDialog(0);
        //TODO Show "Registering..." dialogue???

    }
}

This is successfully invoking the registration process. However I would like to show a “processing dialog” whilst it is going through the registration process and a “confirmation dialogue” when it has successfully registered.

I have another class ‘C2DMReceiver’ which handles the registration with the google servers and also uses sockets to register onto my own servers.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;

public class C2DMReceiver extends BroadcastReceiver {
    private static String KEY = "c2dmPref";
    private static String REGISTRATION_KEY = "registrationKey";

    private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(context, intent);
        } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(context, intent);
        }
    }

    private void handleRegistration(Context context, Intent intent) {
        String registration = intent.getStringExtra("registration_id");
        if (intent.getStringExtra("error") != null) {
            // Registration failed, should try again later.
            Log.d("c2dm", "registration failed");
            String error = intent.getStringExtra("error");
            if(error == "SERVICE_NOT_AVAILABLE"){
                Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
            }else if(error == "ACCOUNT_MISSING"){
                Log.d("c2dm", "ACCOUNT_MISSING");
            }else if(error == "AUTHENTICATION_FAILED"){
                Log.d("c2dm", "AUTHENTICATION_FAILED");
            }else if(error == "TOO_MANY_REGISTRATIONS"){
                Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
            }else if(error == "INVALID_SENDER"){
                Log.d("c2dm", "INVALID_SENDER");
            }else if(error == "PHONE_REGISTRATION_ERROR"){
                Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
            }
        } else if (intent.getStringExtra("unregistered") != null) {
            // unregistration done, new messages from the authorized sender will be rejected
            Log.d("c2dm", "unregistered");

        } else if (registration != null) {
            Log.d("c2dm", registration);
            Editor editor =
                    context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
            editor.putString(REGISTRATION_KEY, registration);
            editor.commit();
            registerWithServer(registration);
            // Send the registration ID to the 3rd party site that is sending the messages.
            // This should be done in a separate thread.
            // When done, remember that all registration is done.
        }
    }

    private void handleMessage(Context context, Intent intent)
    {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            // String blah = (String) extras.get("POSTFIELDS");
            Log.d("c2dm", "recieved: "+extras.getString("message"));

        }

    }

    private void registerWithServer(String c2dmID) {
        String hostname = "10.0.2.2";
        int port = 54321;

        socketClient client = new socketClient(hostname, port);
        String message = client.sendMessage("registerRSA|c2dmID="+c2dmID+",userID="+RegistrationScreen.userID+",bankID="+RegistrationScreen.bankID+",passcode="+RegistrationScreen.regPass+",deviceID=njfdfdsj389rfb,timestamp=00000,");
        Log.v("SOCKETCLIENT",message);

        //Do actions on input string
        String tokens[] = message.split("\\|");
        System.out.println(tokens[0]);
        //

        if (tokens[0].equals("success")) {
            RegistrationScreen.status=100;

        } else if (tokens[0].equals("error")) {
            int errorID = 0;
            String friendlyErrorMessage = null;
            //Split the , then the =
            String variables[] = tokens[1].split(",");
            for (int i=0; i<variables.length; i++) {
                String tempSplit[] = variables[i].split("=");
                if (tempSplit[0].equals("errorID")) {
                    errorID=Integer.parseInt(tempSplit[1]);
                } else if (tempSplit[0].equals("friendlyErrorMessage")) {
                    friendlyErrorMessage=tempSplit[1];
                } 
            }

            //Update UI to alert of error
            //TextView textViewToChange = (TextView) findViewById(R.id.registerTextHeader);
            //textViewToChange.setText("Error getting seed! (ERR" + errorID + ") " + friendlyErrorMessage);
            RegistrationScreen.status=200;

        } else {
            RegistrationScreen.status=300;

            //unknown problem
        }
    }

}

As you can see I have tried to achieve it using a while loop checking a ‘status’ variable, however this causes the program to hang and doesn’t seem like very efficient programming.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:33:37+00:00Added an answer on May 31, 2026 at 6:33 am

    You can use the SharedPreferenceListener in this case.

    Once the user registration is successful, try this one:

      SharedPreferences prefs = PreferenceManager
     .getDefaultSharedPreferences(this.getApplicationContext());
     Editor edit = prefs.edit();
     edit.putBoolean("isRegistered",true).commit();
    

    Once the user unregister is successful, try this one:

      SharedPreferences prefs = PreferenceManager
     .getDefaultSharedPreferences(this.getApplicationContext());
     Editor edit = prefs.edit();
     edit.putBoolean("isRegistered",false).commit();
    

    And in your activity,
    Create a textview (or something of your preference. I’m using textview in this example).
    You can put this code somewhere in OnCreate();

     SharedPreferences prefs = PreferenceManager
     .getDefaultSharedPreferences(this.getApplicationContext());
     OnSharedPreferenceChangeListener listener;
     listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences arg0,
            String key) {
    
            if (key.equalsIgnoreCase("isRegistered")) {
    
                Log.v("RegistrationScreen", "registration status changed");             
                if (prefs.getBoolean("isRegistered", false))
                    {
                      textView.setText("Registered Successfully");
                    }
                    else
                    {
                      textView.setText("Successfully UnRegistered");
                    }
            }
        }
    };
        prefs.registerOnSharedPreferenceChangeListener(listener);
    

    Hope This Helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have got a setup where users can either use a web-based form or
I have got two simple questions How can I tell what server is a
I have got the following problem since the server has safe mode turned on,
I have recently been working with C2DM on a Samsung Galaxy Tab 10.1 running
have got a problem with do this kind of code , can't figure how
I have got a text field, and then I type some text onto it.
I have got this error message: #2002 - The server is not responding (or
I have got myself a server, and on it I'd like to host my
I have one question about C2DM, I registered yesterday and I got email that
I have got UIPickerview in my application working fine. I want to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.